简体   繁体   English

具有异步数据库读取功能的 DialogFlow Firebase Cloud Function

[英]DialogFlow Firebase Cloud Function with async database read

I am using Firebase Cloud Function as fulfilment for my DialogFlow Google Assistant Action, but I need to retrieve data from the Firebase Database before I manage the intent.我使用 Firebase Cloud Function 作为我的 DialogFlow Google Assistant Action 的实现,但我需要在管理意图之前从 Firebase 数据库中检索数据。 Here's a code snippet:这是一个代码片段:

  var userDataRef = sessionDatabaseRef.child(sessionId);
  userDataRef.once("value").then(function(data) {
    console.log(data.val());
    handleIntentAndProcessResponse();
  }).catch(function(){
    console.log("No data yet for this session");
    handleIntentAndProcessResponse();
  });

The function called handleIntentAndProcessResponse is where the intent logic returns the response by setting the conv.ask(new SimpleResponse(blah)) stuff.名为handleIntentAndProcessResponse的函数是意图逻辑通过设置conv.ask(new SimpleResponse(blah))内容返回响应的conv.ask(new SimpleResponse(blah)) When I test this it fails and the Cloud Function log gives me this error:当我测试它失败时,云函数日志给了我这个错误:

Error: No response has been set.错误:未设置响应。 Is this being used in an async call that was not returned as a promise to the intent handler?这是在异步调用中使用的,而不是作为对意图处理程序的承诺返回的吗?

So, how can I handle this async call to the Firebase database so it waits for the response?那么,我该如何处理这个对 Firebase 数据库的异步调用,以便它等待响应? I need to use the data it returns when I handle the intent.我需要在处理意图时使用它返回的数据。

As the error message suggests, you need to return the Promise itself so the dispatcher knows it needs to wait for every part of the async operation to be completed.正如错误消息所暗示的那样,您需要返回 Promise 本身,以便调度程序知道它需要等待异步操作的每个部分完成。

Fortunately, the calls to userDataRef.once().then().catch() will evaluate to a Promise, and you can just return this.幸运的是,对userDataRef.once().then().catch()的调用将评估为一个 Promise,你可以直接返回它。 So this should be good所以这应该是好的

  return userDataRef.once("value").then(function(data) {
    console.log(data.val());
    handleIntentAndProcessResponse();
  }).catch(function(){
    console.log("No data yet for this session");
    handleIntentAndProcessResponse();
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM