I'm working for a project for a course where I need to chain multiple API calls together. Currently everything in the first API call function executes and shows in the terminal but everything after the first API call doesn't do anything. I'm taking the user input of location and date in a form. The first API call works. It translates the location into a latitude and longitude and then seperates the date (MM/DD/YYYY) into three variables, month, date and year and stores them in my projectData object which I then need to use to make the second API call. The problem is that after the first API call function executes and stores everything into the projectData object, everything after that does nothing. I try to log the new projectData object with all the stored information from the first API call but I don't see it logged in the terminal. Then I try to extract the newly stored data from projectData and use it in the second API call function but it does nothing. This is the relevant code:
GEONAMES_API = process.env.GEONAMES_ID
darksky_key = process.env.DARKSKY_KEY
console.log(darksky_key)
console.log(GEONAMES_API);
projectData = {one: {coord: {lat: '', lng: ''}, date: ''}, two: {month: '', day: '', year: ''}, three: {}};
place = {};
app.post('/postData', getData);
function getData(req, res){
console.log(req.body.data);
place = req.body.data.place;
date = req.body.data.date;
projectData.one.date = date;
console.log(projectData)
console.log(date)
console.log(place)
geonames(place, GEONAMES_API)
console.log(projectData)
lat = projectData.one.coord.lat;
lng = projectData.one.coord.lng;
month = projectData.two.month;
day = projectData.two.day;
year = projectData.two.year;
darkskyFetch(lat, lng, month, day, year, darksky_key)
geonames function:
function geonames(place, id){
fetch(`http://api.geonames.org/searchJSON?name=${place}&username=${id}`)
.then(response => response.json())
.then(data => {
console.log(data)
lng = data.geonames[1].lng
lat = data.geonames[1].lat
console.log(lng)
console.log(lat)
projectData.one.coord.lat = lat;
projectData.one.coord.lng = lng;
console.log(projectData)
console.log(projectData)
lat = projectData.one.coord.lat;
lng = projectData.one.coord.lng;
date = projectData.one.date;
console.log(lat)
console.log(lng)
date_split = date.split("/")
console.log(date_split)
month = date_split[0]
day = date_split[1]
year = date_split[2]
console.log(month)
console.log(day)
console.log(year)
projectData.two.month = month;
projectData.two.day = day;
projectData.two.year = year;
console.log(projectData)
})
.catch(error => error)
}
darkskyFetch function:
function darkskyFetch(lat, lng, month, day, year, darksky_key){
fetch(`https://api.darksky.net/forecast/${darksky_key}/${lat},${lng},${year}-${month}-${day}`)
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => eerror)
}
How do I get all the code after my geonames function to execute?
Thanks alot, Michael
If you want to know when the then
callbacks have executed in geonames
, then make sure to return the promise, and chain a then
after its call.
So in geonames
:
return fetch( .....
// ^^^^^^
And where it is called:
geonames(place, GEONAMES_API).then(() => {
// all code that needs to execute after the geonames promise resolves
});
Some remarks:
It is not such a good practice to mutate global variables in your then
callback. Instead, let that then
callback return any new/calculated values. This will become available as the resolved value in the callback that is provided to the geonames().then
.
The syntax becomes a bit easier with async
and await
. So you might want to look into that as well.