Use Request Module HTTP GET Firebase Function Node.js

941 Views Asked by At

I am using Firebase Cloud functions priced plan (!) for a webhook for google actions. I am trying to implement a HTTP GET with the request-module. I have installed the module with npm install request and deployed correctly. I want to use google maps distance-matrix api.

For some reasons I am not able to use the request module at all. In the log I neither see "ERROR in GET" nor "being SUCCESSFULL". Any idea what the issue might be?

// Handle the Dialogflow intent named 'Default Welcome Intent'.
app.intent('Default Welcome Intent', (conv) => {

  var url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=AIzaSyBich-7OBAxvtAwX5XnHQyJ7xZiJ8libVQ";

  request({url: url, json: true}, (err, resp, body) => {
    if (err) {
      console.log('ERROR in GET');
      conv.ask('ERROR in GET');
    }
    else { 
      conv.ask('beeing SUCCESSFULL');
      console.log('beeing SUCCESSFULL');
    }
  })

Thank you in advance and best regards. OliDev

1

There are 1 best solutions below

3
On BEST ANSWER

Here when you are doing async work inside handler function , you have to return a Promise that does that work , because otherwise there would be empty response and error. so

app.intent('Default Welcome Intent', (conv) => {
     return Promise(function(resolve,reject){
          request({url: url, json: true}, (err, resp, body) => {
               if (err) {
                  console.log('ERROR in GET');
                  conv.ask('ERROR in GET');
               }else { 
                  conv.ask('beeing SUCCESSFULL');
                  console.log('beeing SUCCESSFULL');
               }
               resolve()
          })   
     })
})

Hope this works for you.