我正在使用 React、Expo 制作一个简单的天气报告应用程序。但是,有 axios 错误。这是我的代码。 我已经在 stackkoverflow 上查看了相关的帖子,但那些没有用。
React Native unhandled promise rejection network error
Unhandled promise rejection: TypeError: Network request failed expo node backend
export default class extends React.Component{
state={
isLoading : true,
}
getWeather = async(latitude,longitude) => {
const url = `api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`;
const {data: {
main :{ temp },weather}} = await axios.get(url);
this.setState({
isLoading:false,
temp: data.main.temp,
condition:weather[0].main,});
}
getLocation = async() => {
try {
await Location.requestForegroundPermissionsAsync();
const {coords : {latitude, longitude}} = await Location.getCurrentPositionAsync();
this.getWeather(latitude,longitude);
}
catch (error)
{
Alert.alert("Can't find you.", "So sad");
}
}
componentDidMount(){
this.getLocation();
}
render(){
const {isLoading, temp, condition} = this.state;
return isLoading ? <Loading /> : <Weather temp={Math.round(temp)} condition={condition} />
}
}
我已经检查过 url 是否有问题,但它工作正常。问题出在 axios
这是错误消息。
Unhandled promise rejection: Error: Network Error]
在 node_modules\axios\lib\core\settle.js:14:6 在解决
在 node_modules\axios\lib\adapters\xhr.js:65:6 在 handleLoad
在 node_modules\event-target-shim\dist\event-target-shim.js:818:20 在 EventTarget.prototype.dispatchEvent
在 node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 在 setReadyState
在 node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 在 __didCompleteResponse
在 node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 在发射
在 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 在 __callFunction
在 __guard$argument_0 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6
在 __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10
在 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 在 callFunctionReturnFlushedQueue
at [native code]:null in callFunctionReturnFlushedQueue
我使用的地址是“exp://172.16.218.87:19000”
这是我的 package.json 文件。
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"axios": "^0.21.1",
"expo": "~41.0.1",
"expo-location": "~12.0.4",
"expo-status-bar": "~1.0.4",
"prop-types": "^15.7.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz",
"react-native-web": "~0.13.12"
},
"devDependencies": {
"@babel/core": "^7.9.0"
},
"private": true
}
谢谢!!