【发布时间】:2018-09-07 09:34:09
【问题描述】:
我正在为 Alexa 开发书签技能,以自学 DynamoDB。我已经克服了各种障碍,现在可以写信给我的桌子了。问题是每当我 putItem 它添加两个项目。我正在尝试存储用户 ID(DynamoDB 中的分区键)、请求的时间戳(作为字符串和 DynamoDB 中的排序键)、书名和用户所在的页面。自从我尝试使用复合键后,这个问题才开始出现,但我认为我需要这两个字段来 a) 获取唯一的主键 b) 能够找到用户保存的最后一项。
这是我在 Lambda 中的意图代码:
'addBookmark': function() {
//delegate to Alexa to collect all the required slot values
var filledSlots = delegateSlotCollection.call(this);
//Get slot values as variables
var userID = this.event.session.user.userId;
var pageNumber = this.event.request.intent.slots.pageNumber.value;
var bookTitle = this.event.request.intent.slots.bookTitle.value;
//DynamoDB expects the timestamp as a string, so we convert it
var timeStamp = Date.now().toString();
var params = {
TableName: 'bookmarkV6',
Item: {
'userID' : {S: userID},
'timeStamp': { S: timeStamp },
'bookTitle': { S: bookTitle },
'pageNumber': { N: pageNumber },
}
};
//Call DynamoDB to add the item to the table
ddb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
}
else {
console.log("Success", data);
}
});
const speechOutput = "OK, I've made a note that you're on page " + pageNumber + " of " + bookTitle + ".";
this.response.cardRenderer("Bookmark", "Page " + pageNumber + " of " + sentenceCase(bookTitle) +"\n \n" + stringToDate(timeStamp));
this.response.speak(speechOutput);
this.emit(':responseReady');
},
“重复”项目的时间戳值略有不同。
【问题讨论】:
-
我会调试以验证
addBookmark被调用了多少次。如果您确定它只被调用一次,那么我会查看表流。也许你有一个流消费者每次都复制条目? -
感谢您的回复。我不知道如何调试...它会在日志中的某个地方吗?
-
好的,我在 Cloudwatch 中找不到日志,但最后我通过复制 Alexa Skills 工具包中的输入 JSON 创建了一个测试事件。这在我的函数下方给了我一个“正常”的 Lambda 日志。它似乎只触发一次。但是,我尝试删除 varfilledSlots = delegateSlotCollection.call(this);行并解决了重复项目问题。我的猜测是我以某种方式错误地实现了这一点,即使它们都已填满,它也会返回寻找丢失的插槽,因此 dynamoDB putItem 运行了两次。
标签: aws-lambda amazon-dynamodb alexa