React Native:可能未处理的承诺拒绝

Age*_*bra 49 react-native

我收到以下错误: Possible unhandled promise rejection (id:0: Network request failed

这是承诺代码,我不知道这里有什么问题,任何想法?

  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);
    });
}
Run Code Online (Sandbox Code Playgroud)

**编辑:我添加了一个catch函数并得到了更好的错误 You passed an undefined or null state object; instead, use forceUpdate(). index.ios.js:64 undefined

这是index.ios.js代码.网址很好,并给我正确的json数据.我可以用控制台日志中都可以看到region.latitude,并region.longitude处于可用Api(region.latitude, region.longitude).但是data未定义.我仍然不确定发生了什么,为什么会出现问题data以及为什么它未定义.

// var React = require('react-native'); --deprecated

// updated
import React from 'react';

// updated
import {
  AppRegistry,
  MapView,
  View,
  Text,
  StyleSheet,
} from 'react-native';

/*
var {
  AppRegistry,
  MapView,
  View,
  Text,
  StyleSheet
} = React;
*/ // -- depreciated 


var Api = require('./src/api');

var Weather = React.createClass({
  getInitialState: function() {
    return {
      pin: {
        latitude: 0,
        longitude: 0
      },
      city: '',
      temperature: '',
      description: ''
    };
  },
  render: function() {
    return <View style={styles.container}>
      <MapView
        annotations={[this.state.pin]}
        onRegionChangeComplete={this.onRegionChangeComplete}
        style={styles.map}>
      </MapView>
      <View style={styles.textWrapper}>
        <Text style={styles.text}>{this.state.city}</Text>
        <Text style={styles.text}>{this.state.temperature}</Text>
        <Text style={styles.text}>{this.state.description}</Text>
      </View>
    </View>
  },
  onRegionChangeComplete: function(region) {
    this.setState({
      pin: {
        longitude: region.longitude,
        latitude: region.latitude
      }
    });

    Api(region.latitude, region.longitude)
      .then((data) => {
        console.log(data);
        this.setState(data);
      });
  }
});




var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'stretch',
    backgroundColor: '#F5FCFF'
  },
  map: {
    flex: 2,
    marginTop: 30
  },
  textWrapper: {
    flex: 1,
    alignItems: 'center'
  },
  text: {
    fontSize: 30
  }
});

AppRegistry.registerComponent('weather', () => Weather);
Run Code Online (Sandbox Code Playgroud)

whi*_*le1 42

你的api中的catch函数应该返回一些数据,这些数据可以通过React类中的Api调用来处理,或者抛出新的错误,这应该通过在React类代码中使用catch函数来捕获.后者的方法应该是:

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;
});
Run Code Online (Sandbox Code Playgroud)

然后在你的React类中:

Api(region.latitude, region.longitude)
  .then((data) => {
    console.log(data);
    this.setState(data);
  }).catch((error)=>{
     console.log("Api call error");
     alert(error.message);
  });
Run Code Online (Sandbox Code Playgroud)

  • 你在console.log('你的fetch操作有问题:'+error.message)之后添加了抛出错误吗?在您的 fetch catch 函数中。请参阅第一个片段中添加的抛出错误。 (2认同)

小智 6

您应该将 添加catch()到 API 调用的末尾。当您的代码命中时,catch它不会返回任何内容,因此当您尝试使用setState()数据时,数据是未定义的。错误消息实际上也告诉了您这一点:)。