javascript - 将 dynamodb 响应数据传递给回调函数时出现问题

标签 javascript node.js aws-lambda amazon-dynamodb

friend 们,我一直在用头撞 table 。我正在编写 Alexa+Lambda+Dynamodb 操作/事件字符串。问题出现在我的 Lambda Node.JS 中。我有 getChores 函数,其中我尝试运行一个简单的 getItem() 来提取 dynamodb 中的示例条目,但是我只能访问 Else 语句中返回的数据,但我需要通过回调传递它语音输出。如果您能提供任何帮助,我们将不胜感激!

function getChores(callback) {
    sessionAttributes = {};

    var params = {
        TableName: 'Chores',
        Key: {
            'chore': {
                S: 'Clean up toys'
            },
        }
    };

    // Call DynamoDB to read the item from the table
    var results = ddb.getItem(params, function(err, data) {
        if (err) {
            console.log("Error", err);
        } else {
            //CAN ONLY ACCESS HERE!
            console.log("Success", data.Item);
            speechOutput = data.Item.chore.S;
            console.log(data.Item.chore.S);
            console.log(speechOutput);
        }

    });

    console.log(results);

    //Get card title from data
    const cardTitle = "Chore"

    //Get output from data
    //const speechOutput = element.chore;
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    const repromptText = '';
    const shouldEndSession = false;
    callback(sessionAttributes,
        buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

最佳答案

看起来 ddb.getItem 是一个异步方法。您需要在 else block 内调用回调方法“callback”。

关于javascript - 将 dynamodb 响应数据传递给回调函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51369875/

相关文章:

java - AWS Lambda Java 如何读取属性文件

javascript - 如何将 HTML 添加到此 Knockout.js Viewmodel 变量?

javascript - 关闭谷歌应用程序脚本中的窗口

javascript - Java 类和 JavaScript 类之间的区别?

java - AWS Lambda Java函数成功但超时

node.js - 在 AWS lambda 上列出 cognito 用户池用户

javascript - Firebase + js( Angular )离开窗口时更改在线状态

javascript - Vue 3 react 性未从类实例内部触发

node.js - 使用 Node.js 通过 MQTT 连接到 Google Cloud IoT 时遇到问题

javascript - express.js 限制 api 访问仅来自同一网站的页面