Hi I am trying to fetch the weather through openwheather api and post it on a web page. However, in the process of testing this, I found a part that could cause problems.
var my_weather = {};
request.get(
"http://api.openweathermap.org/data/2.5/weather?lat=37.3578631&lon=126.9395806&appid=40e3257323ba173186324d66fac45a1f",
function (req, res) {
data = JSON.parse(res.body);
my_weather.weather_main = data.weather[0].main;
my_weather.temp = Math.round(data.main.temp - 273);
my_weather.humidity = data.main.humidity;
console.log(my_weather);
}
);
console.log(my_weather);
When this code is executed, the outer console.log() is executed before the console.log() in the request function, and an empty object is displayed. How to wait for the weather to be saved in the code above and then execute the code below it. Is there any? I tried using async await, but it didn't work, so please tell me how