显示 openweathermap 错误时出错 API
Error in showing error for openweathermap API
我在我的 React 应用程序中使用 openweathermap.org api。它工作正常,但当用户输入错误的城市名称时不会显示错误。
有时也会显示
cannot read property 'message' of undefined
我的openweather.js
import axios from 'axios';
const weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid=************&units=metric';
export default {
getTemp(location){
var encodedLocation = encodeURIComponent(location);
var requrl = `${weather_url}&q=${encodedLocation}`;
return axios.get(requrl).then(function(res){
if(res.data.cod && res.data.message){
throw new Error(res.message);
} else{
return res.data.main.temp;
}
},function(res){
return new Error(res.data.message);
});
}
}
反应文件:
openWeatherMap.getTemp(location)
.then(function(temp){
that.setState({
location:location,
temp:temp,
isLoading:false
})
},function(err){
alert(err);
});
这是您的代码版本,没有任何未处理的错误 - 基于 axios documentation and openweathermap API documentation
openweather.js
import axios from 'axios';
const weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid=************&units=metric';
export default {
getTemp(location){
var encodedLocation = encodeURIComponent(location);
var requrl = `${weather_url}&q=${encodedLocation}`;
return axios.get(requrl).then(res => {
if (res.data.cod === 200){
return JSON.stringify(res.data);//.data.main.temp;
}
throw res.data.cod;
}, res => {
throw (res && ((res.response && res.response.data && (res.response.data.message || res.response.data)) || (res.code))) || res;
});
}
}
反应文件:
openWeatherMap.getTemp(location).then(function(temp){
that.setState({
location:location,
temp:temp,
isLoading:false
})
},function(err){
alert(err);
});
throw 看起来很丑陋,但我不知道你可能会遇到什么类型的错误...丑陋是基于错误我可以 "force" 通过各种方式
我在我的 React 应用程序中使用 openweathermap.org api。它工作正常,但当用户输入错误的城市名称时不会显示错误。 有时也会显示
cannot read property 'message' of undefined
我的openweather.js
import axios from 'axios';
const weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid=************&units=metric';
export default {
getTemp(location){
var encodedLocation = encodeURIComponent(location);
var requrl = `${weather_url}&q=${encodedLocation}`;
return axios.get(requrl).then(function(res){
if(res.data.cod && res.data.message){
throw new Error(res.message);
} else{
return res.data.main.temp;
}
},function(res){
return new Error(res.data.message);
});
}
}
反应文件:
openWeatherMap.getTemp(location)
.then(function(temp){
that.setState({
location:location,
temp:temp,
isLoading:false
})
},function(err){
alert(err);
});
这是您的代码版本,没有任何未处理的错误 - 基于 axios documentation and openweathermap API documentation
openweather.js
import axios from 'axios';
const weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid=************&units=metric';
export default {
getTemp(location){
var encodedLocation = encodeURIComponent(location);
var requrl = `${weather_url}&q=${encodedLocation}`;
return axios.get(requrl).then(res => {
if (res.data.cod === 200){
return JSON.stringify(res.data);//.data.main.temp;
}
throw res.data.cod;
}, res => {
throw (res && ((res.response && res.response.data && (res.response.data.message || res.response.data)) || (res.code))) || res;
});
}
}
反应文件:
openWeatherMap.getTemp(location).then(function(temp){
that.setState({
location:location,
temp:temp,
isLoading:false
})
},function(err){
alert(err);
});
throw 看起来很丑陋,但我不知道你可能会遇到什么类型的错误...丑陋是基于错误我可以 "force" 通过各种方式