DynamoDB - 关键元素与架构不匹配 [英] DynamoDB - Key element does not match the schema

查看:14
本文介绍了DynamoDB - 关键元素与架构不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新我的 Dynamodb 表 +Users+ 中的一个项目.我尝试了许多不同的方法,但我总是收到相同的错误消息:

I'm trying to update an Item in my Dynamodb Table +Users+. I have tried many different ways but I always received the same error message:

提供的关键元素与架构不匹配

The provided key element does not match the schema

项目的创建和查询一样有效,但更新无效.当我检查 DynamoDB 时,用户创建良好:

The creation of an Item works, as well as a query but not the update. When I check on DynamoDB the user is well created:

{
  "email": "test@email.com",
  "password": "123",
  "registration": 1460136902241,
  "verified": false
}

这是表格信息:

  • 表名:用户
  • 主分区键:电子邮件(字符串)
  • 主排序键:注册(编号)
  • Table name: Users
  • Primary partition key: email (String)
  • Primary sort key: registration (Number)

这是代码(从 lambda 调用):

exports.handler = function(event, context)
{
    var AWS = require("aws-sdk");


    var docClient = new AWS.DynamoDB.DocumentClient();

    var params = {
        TableName: "Users",
        Item:{
            email: "test@email.com",
            password: "123",
            verified: false,
            registration: (new Date()).getTime(),
        }
    };

    // Create the user.

    docClient.put(params, function(err, data)
    {
        if (err)
        {
            context.fail("Put failed...");
            return;
        }

        var params = {
            TableName: "Users",
            Key: { email : "test@email.com" },
            AttributeUpdates: {
                verified: {
                    Action: "PUT",
                    Value: true
                }
            }
        };

        // Update the user.
        docClient.update(params, function(err, data)
        {
            if (err)
            {
                console.log(JSON.stringify(err));
                context.fail(JSON.stringify(err));
                return;
            }
            context.succeed("User successfully updated.");
        });


    });

};

您知道我的代码有什么问题吗?

Do you have any idea of what could be wrong in my code?

推荐答案

你只提供了一半的主键.您的主键是分区键和范围键的组合.您需要在更新参数的 Key 属性中包含范围键.

You are only providing half of your primary key. Your primary key is a combination of the partition key and range key. You need to include the range key in your Key attribute in the update parameters.

这篇关于DynamoDB - 关键元素与架构不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆