簡體   English   中英

Alexa,nodejs使用Lambda通過不同的參數多次查詢DynamoDB表。 異步功能

[英]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之間),並在每次運行時在日期中添加一個。 理想情況下,我想將標題存儲在一個數組中,將時間存儲在另一個數組中(就像我已經在做的那樣)。 我操作日期值沒有問題,我的問題是從函數的一次運行中返回結果並將其放入一個數組,該數組已從函數的上一次運行中填充。

我希望這是有道理的。 任何幫助或指導將不勝感激。

您似乎將日期作為分區鍵,將時間作為范圍鍵。

選項:

  • 使用每個日期多次查詢(您已經建議過)
  • 添加一個新的datetime屬性,然后使用掃描而不是查詢來返回兩個值之間的datetime。 這將評估表中的每一行,但是由於這聽起來像您需要執行的操作,因此這將是一個不錯的方法
  • 如果存在不同的自然分區鍵(即,您只想查詢表的一部分),則可以更改分區鍵並將datetime設置為范圍鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM