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);
});
}