【发布时间】:2020-01-11 00:41:17
【问题描述】:
我正在尝试从 foreach 循环中的 axios 请求中获取数据,但每次都未定义。如果我将 console.log 移到 .then 函数中,它可以工作,但是在循环之后,它返回空数组。我确实发现它正在重新进入循环,然后才保存到数组中。我该如何解决?
var temp = [];
for(var route of coordinates) {
axios('https://api.openweathermap.org/data/2.5/weather?lat=' + route.position.latitude + '&lon=' + route.position.longitude + '&units=metric&appid=39f1004c7ecc22d6f734974c44428625')
.then((response)=>{
temp.push({lat: route.position.latitude, lon: route.position.longitude, temp: Math.round(response.data.main.temp)});
})
.catch((error)=>{
console.log(error)
})
}
console.log(temp);
【问题讨论】:
标签: node.js