当我将Promise与服务器中的dialogflow库一起使用时-获取错误 [英] When I use Promise with dialogflow library in server - get Error

查看:61
本文介绍了当我将Promise与服务器中的dialogflow库一起使用时-获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  const函数= require('firebase-functions');const admin = require('firebase-admin');const firebase_database = require('./conf/firebase');const {WebhookClient} = require('dialogflow-fulfillment');exports.dialogflowFirebaseFulfillment =函数.https.onRequest((request,response)=> {函数searcheColleagueByName(agent){var lastname = agent.parameters.lastname;firebase_database.ref().once('value').then(team => {agent.add("some name" + lastname);}).catch(err => {agent.add(出问题了");})} 

,当我从电报向我的机器人发出请求后,在Firebase控制台中出现错误:

 错误:未为平台定义任何响应:null在V2Agent.sendResponses_(/srv/node_modules/dialogflow-fulfillment/src/v2-agent.js:243:13)在WebhookClient.send_(/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:505:17)然后承诺(/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:316:38)在< anonymous>在process._tickDomainCallback(内部/进程/next_tick.js:228:7) 

怎么了?为什么当我使用 promite 时,我的经纪人无法给我答案?

解决方案

问题是,如果您使用的是异步函数,则您的意图处理程序还必须返回Promise.仅将答复作为 then()子句的一部分发送,还必须返回 then()属于其中的承诺.

就您而言,这看起来相当简单.在 searchColleagueByName()函数中,您将返回 once().then().catch()结果,这是一个承诺.(由于 then() catch()返回了Promise.)

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

 返回firebase_database.ref().once('value').then(team => {agent.add("some name" + lastname);}).catch(err => {agent.add(出问题了");}) 

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?

解决方案

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");
   })

这篇关于当我将Promise与服务器中的dialogflow库一起使用时-获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆