将dynamodb响应数据传递给回调函数的问题

时间:2018-07-16 20:46:31

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

朋友,我一直把头撞在桌子上。我正在编写动作/事件的Alexa + Lambda + Dynamodb字符串。问题出在我的Lambda Node.JS中。我有一个函数getChores,我试图在其中运行一个简单的getItem()以仅提取一个dynamodb中的样本条目,但是我只能访问Else语句中返回的数据,但我需要通过回调传递它speechOutput。您能提供的任何帮助将不胜感激!

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));
}

1 个答案:

答案 0 :(得分:1)

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