In order to generate the weather URL, we'll copy the URL from the weather file, taking it with the ticks in place, and moving it into the app-promise file. We'll make a new variable called weatherUrl, setting it equal to the copied URL:
url: `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`,
Now weatherUrl does need a few pieces of information. We need the latitude and longitude. We have two variables lat and lng, so let's create them, getting the appropriate value from that response object, var lat and var lng:
var lat;
var lng;
url: `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`,
Now in order to pull them off, we have to go through that process of digging into the object. We've done it before. We'll be looking in the response object at the data property, which is similar to the body in the request library. Then we'll go into results, grabbing the first item and accessing the geometry property, then we'll access location.lat:
var lat = response.data.results[0].geometry.location.lat;
Now similarly, we can add things for the longitude variable:
var lat = response.data.results[0].geometry.location.lat;
var lng = response.data.results[0].geometry.location.lng;
Now before we make that weather request, we want to print the formatted address because that's something the previous app did as well. In our console.log(response.data) statement, and instead of printing response.data, we'll dive into the data object getting the formatted address. This is also on the results array's first item. We'll be accessing the formatted_address property:
var lat = response.data.results[0].geometry.location.lat;
var lng = response.data.results[0].geometry.location.lng;
var weatherUrl = `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`;
console.log(response.data.results[0].formatted_address);
Now that we have our formatted address printing to the screen, we can make our second call by returning a new promise. This is going to let us chain these calls together.