I'm trying to chain two promises together that will enable me to get both the current weather from the open weather map API and display it on a Card div. This portion I already have functioning as it is supposed to however under that I would also like to display the weather for 5 days. I am attempting to chain these two fetch request so that when a user enters a city the current weather continues to be pulled up on the Card div in the middle of my app and the weather for that week will be displayed at the bottom. However, this is my first time trying to chain promises from an API together in order to make a cohesive function happen and currently I do not yet understand enough about this particular concept to know where the breakdown is. I am aware of the async await but because my entire project is contained within weather { I am unsure of the syntax of how to use async await in this case. With what I have now it is just a lot even if it was functioning. But the main issue I notice is that doing data.weekWeatherUrl makes the URL inaccessible (makes it declared but not read and doing the opposite makes data the same way.
JavaScript:
weatherData = {
getWeatherInfo: function (city) {
const currentWeatherUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${currentWeatherApiTag}`;
const weekWeatherUrl = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&units=imperial&appid=${weekForecastApiTag}`;
fetch(currentWeatherUrl)
.then(function(response) {
return response.json();
})
.then(function (data) {
return fetch(data.weekWeatherUrl)
})
.then(function(response) {
return response.json();
})
.then(function(data) {
this.unveilWeather(data);
},
unveilWeather: function (data) {
const {name} = data;
const {icon} = data.weather[0];
const {description} = data.weather[0];
const {temp} = data.main;
const {humidity} = (data.main);
const {speed} = data.wind;
console.log(name, icon, description, temp, humidity, speed);
document.querySelector("#City").innerText = "Weather in " + name;
document.querySelector(".weather-icon").src =`http://openweathermap.org/img/wn/${data.weather[0].icon}.png`;
document.querySelector("#CityWeatherDescription").innerText = description;
document.querySelector("#Temperature").innerText = temp + " °F";
document.querySelector (".humidity-weather-stat").innerText = "Humidity : " + humidity + "%";
document.querySelector(".wind-speed-weather-stat").innerText = "Wind Speed : " + speed + "m/hr";
},
searchCurrentWeather: function () {
this.getCurrentWeatherInfo(document.querySelector("#Search-Bubble").value);
},
};
document.getElementById("SearchBtn").addEventListener("click", function () {
weatherData.searchCurrentWeather();
});
document.getElementById("Search-Bubble").addEventListener("keyup", function (event) {
if (event.key === "Enter") {
weatherData.searchCurrentWeather();
}
});
weatherData.getCurrentWeatherInfo("Brooklyn");
I am brand new to coding and working through self-teaching
thanks!