Alexa、nodejs 使用 Lambda 以不同的参数多次查询 DynamoDB table。异步函数

Alexa, nodejs using Lambda to query DynamoDB table multiple times with different parameters. Asynchronous function

我正在尝试开发一种技能,让 Alexa 能够读出用户指定日期 DynamoDB table 中的信息。我已经设法在 Lambda 中使用 nodejs 查询 DynamoDB table,并且能够 return 所有具有特定日期的项目。请参阅下面的代码:

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 天)内推进和 return 所有项目。由于您只能查询 table 中的主分区键与 'equals' 而不是 'between' 两个值。我怀疑我唯一的选择是 运行 这个函数多次使用不同的参数日期。但是,我正在努力做到这一点,在阅读了一些文章之后,我相信这是由于函数的异步性质造成的。

我需要循环此函数一定次数(1 到 7 之间),每次 运行 时将日期加一。理想情况下,我想将标题存储在一个数组中,将时间存储在另一个数组中(正如我已经在做的那样)。我在操作日期值方面没有问题 我的问题是 return 计算一个 运行 函数的结果并将它们放入一个数组中,该数组已经从前一个 运行 中填充功能。

我希望这是有道理的。任何帮助或指导将不胜感激。

您似乎将日期作为分区键,将时间作为范围键。

选项:

  • 多次查询,使用每个日期(您已经建议过)
  • 添加一个新的日期时间属性,然后使用扫描而不是查询来 return 两个值之间的日期时间。这将评估 table 中的每一行,但由于这听起来像您需要做的,所以这将是一个很好的方法
  • 如果有不同的自然分区键(即您只想查询 table 的一部分),您可以更改分区键并将日期时间设置为范围键。