I'm making a simple weather report app with React, Expo. But, there are axios error. Here are my code. I've cchecked related post on stackkoverflow, but thosee didn't work .
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} />
}
}
I've checked whether the url has the problem , but it worked fine. The problem was on axios
Here are error messages.
Unhandled promise rejection: Error: Network Error]
at node_modules\axios\lib\core\settle.js:14:6 in settle
at node_modules\axios\lib\adapters\xhr.js:65:6 in handleLoad
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
at [native code]:null in callFunctionReturnFlushedQueue
the address that I used is "exp://172.16.218.87:19000"
here is my package.json file.
"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
}
Thanks!!