Javascript 向回调函数传递dynamodb响应数据时出现的问题
朋友们,我的头一直撞在桌子上。我正在写一个Alexa+Lambda+Dynamodb动作/事件串。问题出现在我的Lambda Node.JS中。我有一个函数gethorses,在这个函数中,我试图运行一个简单的getItem来提取dynamodb中的一个示例条目,但是我只能访问Else语句中返回的数据,但是我需要通过speechOutput中的回调传递它。如果您能提供任何帮助,我们将不胜感激Javascript 向回调函数传递dynamodb响应数据时出现的问题,javascript,node.js,aws-lambda,amazon-dynamodb,Javascript,Node.js,Aws Lambda,Amazon Dynamodb,朋友们,我的头一直撞在桌子上。我正在写一个Alexa+Lambda+Dynamodb动作/事件串。问题出现在我的Lambda Node.JS中。我有一个函数gethorses,在这个函数中,我试图运行一个简单的getItem来提取dynamodb中的一个示例条目,但是我只能访问Else语句中返回的数据,但是我需要通过speechOutput中的回调传递它。如果您能提供任何帮助,我们将不胜感激 function getChores(callback) { sessionAttributes
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块内调用回调方法callback