Dialogflow V2 错误 - 异步调用不起作用 - 承诺错误?火力点功能

Dialogflow V2 error - Async call is not working - Promise Error? firebase function

我想从 mongodb 获取一些数据(使用 mongoose 框架)但是无法获取数据,

以前我使用 here to getting data which was working well for me in "action on google v1 中描述的回调方法,但在 v2 中不起作用,

然后我阅读了 that we must need to use promise in order to make async call in "action on google v2",然后我根据@prisoner先生在上述问题中的指导重构了我的代码

你可以在这里看到我的代码:

import * as functions from 'firebase-functions';

import {
    dialogflow, 
    SimpleResponse,
    Suggestions, 
    DialogflowConversation, 
    DialogflowApp
} from 'actions-on-google'

import { Model } from './db'

const app = dialogflow({ debug: false })

app.middleware((conv) => {
    conv["hasScreen"] =
        conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT');
    conv["hasAudioPlayback"] =
        conv.surface.capabilities.has('actions.capability.AUDIO_OUTPUT');
});

app.intent('Get Some Data', (conv) => {    

        console.log("Get Some Data Intent triggered")

        return new Promise(function (resolve, reject) {

            Model.find({}, (err, result: any) => {
                if (!err) {

                    if (!result.length) {

                        console.log("no data found")
                        conv.ask(new SimpleResponse({
                            speech: "no data found"
                        }))
                        resolve();

                    } else {

                        console.log("lots of data found")
                        conv.ask(new SimpleResponse({
                            speech: "lots of data found"
                        }));
                        resolve();

                    }
                } else {
                    console.log("Error in getting data: ", err);
                    conv.ask(new SimpleResponse({
                        speech: "Error in getting data"
                    }))
                    resolve();
                }
            })
        })
});

exports.webhook = functions.https.onRequest(app);

它仍然不适合我,功能超时

实际上我已经启动了我的应用程序并且 运行 在 v1 中正常运行,现在我正在尝试从 v1 迁移到 v2 因为我需要使用 v1 中不可用的一些最新功能,例如语音验证和其他新功能。 任何帮助都将受到热烈欢迎

您引用的页面 at the Mongoose documentation that talks about find() also gives information about how to use it with Promises. The find() call returns a Query and you can use query.exec() 以获取您随后将使用的 Promise。

所以代码可能看起来像这样(未经测试,因为我不使用 Mongoose):

app.intent('Get Some Data', (conv) => {
  var query = Model.find({});
  return query.exec()

    .then( result => {
      if( !result || !result.length ){
        return conv.ask(new SimpleResponse({
          speech: "no data found"
        }));

      } else {
        return conv.ask(new SimpleResponse({
          speech: "some data found"
        }));
      }
    })

    .catch( err => {
      return conv.close(new SimpleResponse({
        speech: "something went wrong"
      }));
    });
});