1

I am trying to query Google Place API for details in order to display a map (by obtaining first coordinates) and location information (address) as a chatbot response. I have written this quote, but I am have having problems with the Google Place API response. Appreciate any help available.

var path1 = `/maps/api/place/details/json?placeid=${placeID}&fields=name,formatted_address,geometry&key=${googleAPI}`;

details = getPlace(path1);          
console.log('details: ' + details);                         //debug statement


function getPlace (path1) {
    const host = 'https://maps.googleapis.com';
    var url = host + path1; 
    var data = null;

    return new Promise(function(resolve, reject) {
        request (url, function (err,response,body) {
            if(!err){
                try{
                    data = JSON.parse(body);
                    resolve(data);
                }catch (err) {
                    console.error('parsing error');
                    reject('Error during parsing of data: ' + err.message);
                }
            } else {
                console.error('request error');
                reject('Unable to get data. Statuscode: ' + response.statusCode);
            }
        });
    }); 
}  

The response I am getting from Firebase is : dialogflowFirebaseFulfillment details: [object Promise]

when I try the URL in browser, I am getting a proper response https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJJ0JHF9A3zDERwazNPMoMKAg&fields=name,formatted_address,geometry&key=API-Key

{
   "html_attributions" : [],
   "result" : {
      "formatted_address" : "Level 1, Smart Banking Area Menara Citibank, 165, Jalan Ampang, Kuala Lumpur, 50450 Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, Malaysia",
      "geometry" : {
         "location" : {
            "lat" : 3.1600754,
            "lng" : 101.717633
         },
         "viewport" : {
            "northeast" : {
               "lat" : 3.161558980291502,
               "lng" : 101.7188249302915
            },
            "southwest" : {
               "lat" : 3.158861019708498,
               "lng" : 101.7161269697085
            }
         }
      },
      "name" : "Citibank Malaysia - Kuala Lumpur"
   },
   "status" : "OK"
}

Not sure what is the problem here, anyone able to help?

Puppy
  • 11
  • 2

1 Answers1

0

It may be because you are not awaiting the Promise in getPlace() to resolve before continuing with your code.

Try making the function you are calling getPlace() from async and using await when calling the function. For example:

async function parentFunction() {
  var path1 = `/maps/api/place/details/json?placeid=${placeID}&fields=name,formatted_address,geometry&key=${googleAPI}`;

  details = await getPlace(path1);          
  console.log('details: ' + details);

}

Here's a good post that explains async code and how to deal with it

Darren G
  • 780
  • 8
  • 16
  • Firebase does not recognize the keywords `async` and `await`. – Puppy Jul 24 '19 at 08:16
  • I tried adding additional debug lines: `console.log(body); data = JSON.parse(body); console.log(data);` but there is no log entries in console, I suspect that it is not being executed – Puppy Jul 24 '19 at 08:17
  • I'm not too familiar with firebase, but I'd say your problem is definitely promise related. The data is empty likely because console.log() is running before the data has been returned from Google and the promise resolved. If async/await aren't supported, you could try: getPlace(path1).then(console.log('details: ' + details); This will wait for the promise to resolve before logging the result. – Darren G Jul 25 '19 at 03:06