react-native - react native : Possible unhandled promise rejection

标签 react-native

我收到以下错误:

Possible unhandled promise rejection (id:0: Network request failed)

这是 promise 代码,我不明白这里有什么问题,有什么想法吗?

  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);
    });
}

编辑:

我添加了一个 catch 函数并得到了更好的错误:

You passed an undefined or null state object; instead, use forceUpdate(). index.ios.js:64 undefined

这是index.ios.js 代码。 url 很好,给了我正确的 json 数据。我可以通过控制台日志看到 region.latituderegion.longitudeApi(region.latitude, zone.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);

最佳答案

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;
});

然后在你的 React 类中:

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

关于react-native - react native : Possible unhandled promise rejection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842499/

相关文章:

android - 在物理设备而非模拟器上运行时,React-Native应用程序崩溃

react-native - 异步/等待函数返回 _40 : 0, _65 : 0, _55 : null, _72: null

javascript - 如何将 QuickBlox 集成到 React Native 中?

javascript - React Native BackHandler 不跟随状态更新

React-Native:找不到变量:缓冲区

javascript - 有没有办法用 React native 录制音频并将其存储为 wave 文件?

javascript - 在 React Native 中使用 Airbnb 的 "react-with-styles"

react-native - 为什么图像以数字形式导入?

javascript - 分配的响应数据显示 [object object] React Native

javascript - 如何以不同的可见性百分比检查 FlatList 中项目的可见性?