运行putItem时,有两个项目添加到DynamoDB

时间:2018-03-28 15:47:45

标签: aws-lambda amazon-dynamodb alexa

我正在研究Alexa的书签技巧,以自学DynamoDB。我已经克服了各种各样的障碍,现在可以写信给我的桌子了。问题是,每当我putItem它添加两个项目。我试图存储userID(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');

    },

"重复"项目的时间戳值略有不同。

2 个答案:

答案 0 :(得分:0)

我也有同样的问题。使用的委托集合正在发生,但无法解决。我有6个插槽的委托插槽确认,当我给出所有6个插槽的值时,最后我最终在表中有7个记录。

答案 1 :(得分:0)

在delegateSlotCollection()函数中,返回" COMPLETED"在else块和addbookmark意图中,请在delegateSlotCollection.call方法之后检查如下

var filledSlots = delegateSlotCollection.call(this); 如果(filledSlots ==='已完成'){    将所有保存的dynamodb逻辑放在这里。 }