I'm developing Chrome Extension and I want to get weather info with fetch()
but When I save the incoming data named weatherData in my own variable named weatherData, it becomes undefined. That's why the codes are not working correctly. Please help.
API Key is true because console.log(data
) is working well but console.log(weatherData)
is not working.
This content script
:
const hostname = window.location.hostname;
if(hostname.includes("google.com")) {
var weatherData;
document.body.innerHTML +=
"<span id='content_weather'>"+
"<span id='weather_temp'></span>"+
"</span>";
getWeather();
setWeather();
}
function getWeather() {
fetch("https://api.openweathermap.org/data/2.5/weather?lat=39.106141&lon=39.548280&appid={MY API KEY}&units=metric&lang=tr")
.then(response => response.json())
.then(data => weatherData = data);
}
function setWeather() {
document.getElementById("weather_temp").innerHTML = JSON.stringify(weatherData.main.temp);
}