[英]Alexa, nodejs using Lambda to query DynamoDB table multiple times with different parameters. Asynchronous function
我正在嘗試開發一種技能,其中Alexa將從用戶指定的日期讀出DynamoDB表中的信息。 我設法使用Lambda中的nodejs查詢DynamoDB表,並且能夠返回具有特定日期的所有項目。 參見下面的代碼:
function queryDynamoDate_multiple() {
const startdate = this.attributes['startdate'];
var say = '';
var params = {
TableName: 'RegalCinema',
KeyConditionExpression: "#date = :yymmdd and #time between :time1 and :time2",
ExpressionAttributeNames:{
"#date": "date",
"#time": "time"
},
ExpressionAttributeValues: {
":yymmdd":startdate,
":time1":'0',
":time2":'2'
}};
readDynamoItem(params, myResult=>{
say = myResult;
say = 'you asked,. The answer is: ' + myResult;
this.response.speak(say).listen('try again');
this.emit(':responseReady');
});
}
。
function readDynamoItem(params, callback) {
var title = [];
var time = [];
var noofitems = 0;
let speechOutput = "";
docClient.query(params, (err, data) => {
if (err) {
this.emit(':tell', 'Test Error');
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(" -", item.title + ": ");
noofitems = noofitems + 1;
title[noofitems] = item.title;
time[noofitems] = item.time;
});
for (var l = 1; l <= noofitems ; l++){
if ( l== noofitems){
speechOutput = speechOutput +" and "+ title[l] + " at " + time[l] + ". ";
} else if ( l == 1) {
speechOutput = speechOutput + title[l] + " at " + time[l];
} else {
speechOutput = speechOutput + ", " + title[l] + " at "+ time[l];
}
}
callback(speechOutput)
}
});
}
現在,我要繼續處理並退回所有項目(最多7天)。 因為您只能用“等於”而不是“兩個”之間的值查詢表中的主分區鍵。 我懷疑我唯一的選擇是用不同的日期多次運行此函數。 但是,我很難做到這一點,並且在閱讀后我相信這是由於該函數的異步特性。
我需要遍歷此函數一定次數(在1到7之間),並在每次運行時在日期中添加一個。 理想情況下,我想將標題存儲在一個數組中,將時間存儲在另一個數組中(就像我已經在做的那樣)。 我操作日期值沒有問題,我的問題是從函數的一次運行中返回結果並將其放入一個數組,該數組已從函數的上一次運行中填充。
我希望這是有道理的。 任何幫助或指導將不勝感激。
您似乎將日期作為分區鍵,將時間作為范圍鍵。
選項:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.