【发布时间】:2018-08-12 14:09:35
【问题描述】:
我正在尝试开发一项技能,让 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 之间),并在每次运行时添加一个到日期。理想情况下,我想将标题存储在一个数组中,将时间存储在另一个数组中(正如我已经在做的那样)。我在处理日期值时没有问题我的问题是从函数的一次运行中返回结果并将它们放入已经从函数的先前运行中填充的数组中。
我希望这是有道理的。任何帮助或指导将不胜感激。
【问题讨论】:
标签: node.js aws-lambda amazon-dynamodb alexa alexa-skills-kit