【问题标题】:Alexa, nodejs using Lambda to query DynamoDB table multiple times with different parameters. Asynchronous functionAlexa、nodejs 使用 Lambda 以不同的参数多次查询 DynamoDB 表。异步函数
【发布时间】: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


    【解决方案1】:

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

    选项:

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

    【讨论】:

    • 谢谢你!扫描选项似乎正在工作,并且似乎是实现事物的最简单方法。以后得学异步函数了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 2018-05-05
    • 2020-05-23
    • 2019-11-19
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多