I have a list of cities and the city id in my database. I want to loop through the cities and get the current weather of each city in an array using axios. I tried this,
const job = schedule.scheduleJob('*/1 * * * *', async () => {
const cities = await City.find({}, { _id: 0, cityId: 1 });
const current_weather = [];
await cities.forEach((city) => {
axios
.get(`http://api.openweathermap.org/data/2.5/weather?id=${city.cityId}&appid=${process.env.API_KEY}`)
.then((response) => current_weather.push(response))
.catch((error) => console.error(error));
});
console.log(current_weather);
});
But each Axios request is printing Error: connect ENOBUFS - Local (undefined:undefined)
error. Please find a solution to loop through city id and get results in an array. Thank you.