0

Please don't delete my question, I have not found something similar without using ajax. I am new to Nodejs and have been stuck on this for weeks. I am using two APIs and I need the json object that i got form the second API to be used in the first. IT does not recognize the variable even though I have declared them globally (lat and lng).

app.get("/", (req, res) => {
  getPlaces(res);
  getGeo(res);
});


var lat;
var lng;

function getPlaces(res){

  const options = {
    method: 'GET',
    url: 'https://trueway-places.p.rapidapi.com/FindPlacesNearby',
    params: {location: `${lat},${lng}`, type: 'cafe', radius: '150', language: 'en'},
    headers: {
      'X-RapidAPI-Key': '49b3d82605msh88c216e62d3770cp13f60cjsnec91372acf2e',
      'X-RapidAPI-Host': 'trueway-places.p.rapidapi.com'
    }
  };
  
  axios.request(options).then(function (response) {
    console.log(response.data);
    res.render("index", {places: response.data})
  }).catch(function (error) {
    console.error(error);
  });
}

function getGeo(res){
  const options = {
    method: 'GET',
    url: 'https://trueway-geocoding.p.rapidapi.com/Geocode',
    params: {address: 'toronto, ontario', language: 'en'},
    headers: {
      'X-RapidAPI-Key': '49b3d82605msh88c216e62d3770cp13f60cjsnec91372acf2e',
      'X-RapidAPI-Host': 'trueway-geocoding.p.rapidapi.com'
    }
  };
  
  axios.request(options).then(function (response) {
    lat = JSON.stringify(response.data.results[0].location.lat);
    lng = JSON.stringify(response.data.results[0].location.lng);
    console.log(lat, lng);
  }).catch(function (error) {
    console.error(error);
  });
}
  • In what way is this not a dupe of [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992)? What do you mean "without using ajax"? You need the result of one async call for use in another. – Dave Newton Jul 27 '22 at 02:26

1 Answers1

0

Since your function is asynchronous. There are different approaches to achieve this.

  • Using async.js
  • Using Promises
  • async / await

Below is the approach through async and await

app.get("/", (req, res) => {
getLocation(res);
});

async function getLocation(res) {
let {
  lat,
  longitude
} = await getGeo();
getPlaces(lat, longitude, res);

})

function getGeo() {
  try {
    const options = {
      method: 'GET',
      url: 'https://trueway-geocoding.p.rapidapi.com/Geocode',
      params: {
        address: 'toronto, ontario',
        language: 'en'
      },
      headers: {
        'X-RapidAPI-Key': '49b3d82605msh88c216e62d3770cp13f60cjsnec91372acf2e',
        'X-RapidAPI-Host': 'trueway-geocoding.p.rapidapi.com'
      }
    };

    let res = await axios(options)
    if (res.status == 200) {
      // test for status you want, etc
      console.log(res.status)
    }
    // Don't forget to return something   
    return {
      lat: response.data.results[0].location.lat,
      longitude: response.data.results[0].location.long
    }
  } catch (err) {
    console.error(err);
  }
}

function getPlaces(lat, longitude, res) {

  const options = {
    method: 'GET',
    url: 'https://trueway-places.p.rapidapi.com/FindPlacesNearby',
    params: {
      location: `${lat},${longitude}`,
      type: 'cafe',
      radius: '150',
      language: 'en'
    },
    headers: {
      'X-RapidAPI-Key': '49b3d82605msh88c216e62d3770cp13f60cjsnec91372acf2e',
      'X-RapidAPI-Host': 'trueway-places.p.rapidapi.com'
    }
  };

  axios.request(options).then(function(response) {
    console.log(response.data);
    res.render("index", {
      places: response.data
    })
  }).catch(function(error) {
    console.error(error);
  });
}
Gurpinder
  • 634
  • 1
  • 5
  • 18