【问题标题】:Use Request Module HTTP GET Firebase Function Node.js使用请求模块 HTTP GET Firebase 函数 Node.js
【发布时间】:2019-04-14 16:01:36
【问题描述】:

我正在使用 Firebase Cloud 功能定价计划 (!) 作为用于 google 操作的 webhook。我正在尝试使用请求模块实现 HTTP GET。我已经使用 npm install request 安装了模块并正确部署。我想使用谷歌地图距离矩阵 api。

由于某些原因,我根本无法使用请求模块。在日志中,我既没有看到“GET 中的错误”,也没有看到“成功”。知道可能是什么问题吗?

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

提前感谢您并致以最诚挚的问候。 OliDev

【问题讨论】:

    标签: node.js firebase google-cloud-functions google-assist-api


    【解决方案1】:

    在这里,当您在处理函数中执行异步工作时,您必须返回一个完成该工作的 Promise ,否则会出现空响应和错误。所以

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

    希望这对你有用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多