0

trying to use information from an API call to then send a second call.

Here's what I've got:

This is the first API call, I've got it working and rendering; it gathers information for disc golf courses.

async function dgcrGetReq(res, cityName) {
    var courseData = {
        title: "courses",
        courses: null
    };
    await axios.get(`${dgcrUrl}?key=${dgcrKey}&mode=findloc&city=${cityName}&state=ON&country=CA&sig=${torontoSig}`)
    .then(function (response){
        //Run the weather API call
        courseData.courses = response.data;
        res.render("courses", courseData);
    }).catch(function (error){
        console.log(error);
    });
}

Here's the second API call: It collects weather data using latitude and longitude

async function getWeather(res) {
    var weatherData = {
        title: "Weather Information",
        weather: null
    };
    await axios.get(`${weatherUrl}${weatherKey}`)
    .then(function (response){
        weatherData.weather = response.data;
        res.render("weather", weatherData);
    }).catch(function (error){
        console.log(error);
    });
}

What I need is to collect the latitude and longitude elements from the disc golf API and use them as parameters for the Weather app. I know I need to squeeze the second call into the first somehow but I'm unsure of where it actually belongs. I'm using node.js and axios, looking for solutions that don't stray from those technologies.

Links: DGCR API documentation Open Weather Map Documentation

Thanks!

DylanB
  • 119
  • 6
  • https://stackoverflow.com/questions/47956521/chain-requests-so-i-can-use-data-from-my-first-request-in-my-second-one?rq=1 – Asad Awadia Apr 06 '22 at 18:08
  • This looks useful however I'm not using React, just plain Node.js so I'm looking for a solution that works with that constraint. – DylanB Apr 06 '22 at 18:21

1 Answers1

1

As per my understanding you want to call your weather API call from within dgcrGetReq()

First of all please don't mix promises then/catch syntax with async/await

You have to modify getWeather so that it accepts lat/long variables

async function getWeather(lat, long) {
    try {
        let weatherData = {
            title: "Weather Information",
            weather: null
        };

       //You can fix the URL accordingly
        await axios.get(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid={/*Your API key*/}`);
        weatherData.weather = response.data;

        return weatherData;
    } catch (err) {
        console.error(err);
    }

}

Now Call getWeather in here

async function dgcrGetReq(res, cityName) {
    try {
        let courseData = {
            title: "courses",
            courses: null
        };
        const golfData = await axios.get(`${dgcrUrl}?key=${dgcrKey}&mode=findloc&city=${cityName}&state=ON&country=CA&sig=${torontoSig}`);

        courseData.courses = response.data;

        const weatherData = await getWeather(golfData.latitude, golfData.longitude);

        res.render("courses", courseData);
    } catch (err) {
        console.error(err);
    }

}

Not sure what are you trying to achieve with multiple res.render() but you might have to modify your code a bit in order to send all the data.

Shivam
  • 3,514
  • 2
  • 13
  • 27