I am trying to get data from an axios request in a foreach loop, but every time it is undefined. If I move the console.log inside the .then function, it works, but after the loop, it returns empty array. I did find, that it is reentering loop, before it saves to the array. How would I fix that?
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);