【发布时间】:2018-12-16 21:42:43
【问题描述】:
我在集成 OpenWeather 的 api 时遇到了一些问题。
我可以成功记录来自 api 的响应数据值。但是当我使用 .then() 从另一个组件调用时,向我抛出错误。
以下是我用 axios 调用的 api.js
function getFiveDayForcast(cityName) {
axios.get(`http://api.openweathermap.org/data/2.5/forecast/daily?q=${cityName}&type=accurate&APPID=${apiKey}&cnt=5`)
.then(function(response){
// console.log("response" , response.data);
return response.data;
})}
module.exports= {getFiveDayForecast:getFiveDayForecast}
这是我从我的组件回调的时候
componentDidMount(){
let city = queryString.parse(this.props.location.search).city;
this.makeRequest(city);
}
makeRequest =(city) => {
//set loading false
this.setState(()=> ({loading:false}));
api.getFiveDayForecast(city).then((response) => {
console.log(response)
})
}
然后它抛出
TypeError: 无法读取未定义的属性 'then'
如果有人能详细解释一下就好了。我也在阅读文档。提前致谢。
【问题讨论】:
标签: reactjs ecmascript-6