【问题标题】:Issues passing dynamodb response data to callback function将 dynamodb 响应数据传递给回调函数的问题
【发布时间】:2018-07-17 05:01:46
【问题描述】:

朋友们,我一直在用头撞桌子。我正在编写一个 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));
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多