当我在服务器中将 Promise 与 dialogflow 库一起使用时 - 出现错误

When I use Promise with dialogflow library in server - get Error

我尝试在 Firebase Cloud Functions 上执行此代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebase_database = require('./conf/firebase');
const { WebhookClient } = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
     function searcheColleagueByName(agent){
       var lastname = agent.parameters.lastname;        
       firebase_database.ref().once('value')
       .then(team => {
           agent.add("some name " + lastname);
        })
       .catch(err=>{
           agent.add("something wrong");
       })
     }

当我通过电报向我的机器人发出请求后,我在 firebase 控制台中收到错误消息:

Error: No responses defined for platform: null
    at V2Agent.sendResponses_ (/srv/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)
    at WebhookClient.send_ (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)
    at promise.then (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

怎么了?为什么当我使用 promisse 时我的代理不能给我答复?

问题是,如果您使用的是异步函数,那么您的意图处理程序还必须 return 一个 Promise。将回复作为 then() 子句的一部分发送是不够的,您还必须 return then() 是其一部分的 Promise。

对于您的情况,这看起来相当简单。在 searchColleagueByName() 函数中,您将 return 然后 once().then().catch() 结果,这是一个 Promise。 (因为 then()catch() return 一个承诺。)

所以它可能看起来像这样:

   return firebase_database.ref().once('value')
   .then(team => {
       agent.add("some name " + lastname);
    })
   .catch(err=>{
       agent.add("something wrong");
   })