使用带有 Alexa 的 nodejs 查询 dynamodb return 多个项目

query dynamodb using nodejs with Alexa to return multiple items

我对这一切都不熟悉,所以提前致歉。我正在尝试使用 nodejs 和 lambda 查询 Dynamodb,这样我就可以让 Alexa 获得 return 的值。 dynamodb table 设置为如下所示:

date          time      filmname
2018-01-04    13:00     Titanic
2018-01-04    15:30     Titanic
2018-01-04    18:30     Paddingtion 
2018-01-05    12:00     Star Wars

我的 table 设置为: 主分区键 = 日期(字符串) 主排序键 = 时间(字符串)

现在我想做的是从 dynamodb 查询或获取特定日期的信息,因此对于 2018-01-04 3 项应该 return。是否可以在 Lambda 中使用 nodejs 并允许 alexa 读回所有项目?

我已经在我的意图中设置了以下代码并且工作正常:

var params = {
    TableName: 'Cinema',
    Key:{ "date": "2018-01-04", "time" : "13:00"  }
    };

docClient.get(params, (err, data) => {
    if (err) {
        this.emit(':tell', 'Test Error');
    } else {
        this.emit(':tell', 'Test Working ' + data.Item.title);
    }
});

上面的代码returns 泰坦尼克号,符合预期。但是,我对如何将它获取到给定日期的所有项目而不是仅针对该特定时间的 return 感到困惑。

我能够 运行 这个 js 代码独立(即不在 lambda 中)并且这工作正常。尽管我怀疑这不是最好的方法。

 var params = {
            TableName: 'Cinema',
            KeyConditionExpression: "#date = :yymmdd and #time between :time1 and :time2",
            ExpressionAttributeNames:{
            "#date": "date",
        "#time": "time"

            },
            ExpressionAttributeValues: {
            ":yymmdd":"2018-01-04",
        ":time1":'0',
        ":time2":'2'
    }
};


docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("Query succeeded.");
        data.Items.forEach(function(item) {
            console.log(" -", item.title + ": ");
        });
    }
});

现在,如果我 运行 Lambda 中的这段代码并尝试测试我获得的技能 'The remote endpoint could not be called, or the response it returned was invalid.' 另外,由于某种原因,我的 cloudwatch 日志没有针对特定的 lambda 函数进行更新,所以我无法以在此时获得更多信息。

如果有人感兴趣,我解决了这个问题。就像确保发送给 Alexa 的响应只被触发一次一样简单。

我是新手,所以尽管代码按要求工作,但我愿意接受任何关于最佳实践的建议。

function queryDynamoDate_single() {
const startdate = this.attributes['startdate'];


 var say = '';

var params = {
        TableName: 'Cinema',
        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++){
     {
            speechOutput = speechOutput + title[l]; 
     }

callback(speechOutput);
        }
    });

}