React Native: Possible unhandled promise rejection

后端 未结 6 1711
再見小時候
再見小時候 2020-11-28 07:32

I\'m getting the following error: Possible unhandled promise rejection (id:0: Network request failed

Here\'s the promise code, I don\'t see what\'s wro

相关标签:
6条回答
  • 2020-11-28 08:16

    catch function in your api should either return some data which could be handled by Api call in React class or throw new error which should be caught using a catch function in your React class code. Latter approach should be something like:

    return fetch(url)
    .then(function(response){
      return response.json();
    })
    .then(function(json){
      return {
        city: json.name,
        temperature: kelvinToF(json.main.temp),
        description: _.capitalize(json.weather[0].description)
      }
    })
    .catch(function(error) {
    console.log('There has been a problem with your fetch operation: ' + error.message);
     // ADD THIS THROW error
      throw error;
    });
    

    Then in your React Class:

    Api(region.latitude, region.longitude)
      .then((data) => {
        console.log(data);
        this.setState(data);
      }).catch((error)=>{
         console.log("Api call error");
         alert(error.message);
      });
    
    0 讨论(0)
  • 2020-11-28 08:18

    You should add the catch() to the end of the Api call. When your code hits the catch() it doesn't return anything, so data is undefined when you try to use setState() on it. The error message actually tells you this too :)

    0 讨论(0)
  • 2020-11-28 08:19

    In My case, I am running a local Django backend in IP 127.0.0.1:8000 with Expo start. Just make sure you have the server in public domain not hosted lcoally on your machine

    0 讨论(0)
  • 2020-11-28 08:21

    Adding here my experience that hopefully might help somebody.

    I was experiencing the same issue on Android emulator in Linux with hot reload. The code was correct as per accepted answer and the emulator could reach the internet (I needed a domain name).

    Refreshing manually the app made it work. So maybe it has something to do with the hot reloading.

    0 讨论(0)
  • 2020-11-28 08:27

    delete build folder projectfile\android\app\build and run project

    0 讨论(0)
  • 2020-11-28 08:33

    According to this post, you should enable it in XCode.

    1. Click on your project in the Project Navigator
    2. Open the Info tab
    3. Click on the down arrow left to the "App Transport Security Settings"
    4. Right click on "App Transport Security Settings" and select Add Row
    5. For created row set the key “Allow Arbitrary Loads“, type to boolean and value to YES.

    0 讨论(0)
提交回复
热议问题