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/