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

断了今生、忘了曾经 提交于 2021-02-09 07:02:16

问题


I try to execute this code on 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");
       })
     }

and after when i make request to my bot from telegram I get error in firebase console:

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)

What's wrong? why when I use promisse my agent can't give me an answer?


回答1:


The problem is that, if you're using an async function, then your intent handler must also return a Promise. It isn't enough that you send the reply as part of the then() clause, you must also return the Promise that the then() is part of.

In your case, this looks fairly easy. In the searchColleagueByName() function, you would return then once().then().catch() result, which is a Promise. (Since then() and catch() return a Promise.)

So it might look something like this:

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


来源:https://stackoverflow.com/questions/53382012/when-i-use-promise-with-dialogflow-library-in-server-get-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!