const getWeather = (req, res, next)=>{
const cities = req.body.cities;
let weatherInfo = [];
try{
cities.forEach(async(city)=>{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.API_KEY}`);
if(!response.ok){
weatherInfo.push({error : "No Data Found"});
}
const responseData = await response.json();
const cityData = {city : responseData.main.temp - 273.15};
weatherInfo.push(cityData);
});
}
catch(err){
const error = err.message || "Something Went Wrong.. Please Try again!!";
return next(error);
}
res.json(weatherInfo);
}
exports.getWeather = getWeather
I want to push cityData inside the weatherInfo array, but it is not happening. I am always getting empty array as output.