,我發現了以下錯誤: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);
});
}
**編輯:我添加了一個捕捉功能,並得到了更好的錯誤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);
也加上'.catch()',看看錯誤是否消失。參考:https://facebook.github.io/react-native/docs/network.html#handling-the-response – Cherniv
謝謝,這給了我一個更好的錯誤,但沒有擺脫它:DI編輯了上述問題以反映更好的錯誤。 –