首页 文章

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

提问于
浏览
0

我正在研究Alexa的书签技巧,以自学DynamoDB . 我已经克服了各种各样的障碍,现在可以写信给我的 table 了 . 问题是,每当我putItem它添加两个项目 . 我正在尝试存储userID(DynamoDB中的分区键),请求的时间戳(作为字符串,以及DynamoDB中的排序键),书籍的 Headers 和用户所在的页面 . 这个问题从我尝试使用复合键开始才开始,但我想我需要这两个字段来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

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

  • 0

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

    var filledSlots = delegateSlotCollection.call(this); if(filledSlots ==='COMPLETED'){将所有保存的dynamodb逻辑放在这里 . }

相关问题