首页 > 解决方案 > 读取 Firebase 数据会阻止异步方法解析

问题描述

我正在尝试编写从 Firebase 读取的 Alexa 技能

我处于使用 Alexa 测试控制台时调用 NodeJS 方法的位置,但是如果我添加代码以从 Firebase 检索数据,则该方法会挂起,直到 lambda 超时

const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'plantsIntent';
    },
    async handle(handlerInput) {
        const snapshot = (await db.collection('plants').get()).data();
        const names = snapshot.docs.map(doc => doc.data().name);

        const speakOutput = 'Get yourself some cool plants like' + names.join(' and ');
        console.log(speakOutput);

        var response = handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();

        console.log(response);
        return response;
    }
};

当我运行此代码时,我将speakOutput字符串和response对象输出都放入日志中,因此我知道代码正在设法做到这一点

我怀疑这与 Firebase 有关,就好像我删除了db.collection('plants').get()代码段(和相关变量)然后代码运行完成

我怀疑这与方法未返回而不是发生异常有关,因为response在工作版本(没有 Firebase .get())和非工作版本中的输出是相同的

任何帮助,将不胜感激!

标签: node.jsfirebaseasynchronousalexa

解决方案


推荐阅读