Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

amazon web services - DynamoDB - Key element does not match the schema

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

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": "[email protected]",
  "password": "123",
  "registration": 1460136902241,
  "verified": false
}

Here is the table information:

  • Table name: Users
  • Primary partition key: email (String)
  • Primary sort key: registration (Number)

Here is the code (called from lambda):

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


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

    var params = {
        TableName: "Users",
        Item:{
            email: "[email protected]",
            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 : "[email protected]" },
            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?

question from:https://stackoverflow.com/questions/36506546/dynamodb-key-element-does-not-match-the-schema

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...