user3358326
user3358326

Reputation: 55

One of the required keys was not given a value - AWS

I am new to DynamoDB and I was trying to design a table and do some proof of concept. Below is the table I created with Local Secondary Index

var params = {
        TableName : "sample",
        KeySchema: [
            { AttributeName: "user_id", KeyType: "HASH"},
            { AttributeName: "created_date", KeyType: "RANGE"}
        ],
        AttributeDefinitions: [
            { AttributeName: "user_id", AttributeType: "S" },
            { AttributeName: "created_date", AttributeType: "S" },
            { AttributeName: "video_id", AttributeType: "S" }

        ],
        ProvisionedThroughput: {
            ReadCapacityUnits: 5,
            WriteCapacityUnits: 5
        },
        LocalSecondaryIndexes: [{
                IndexName: "user_id-video_id-Index",
                KeySchema: [
                    {
                        AttributeName: "user_id",
                        KeyType: "HASH"
                    },
                    {
                        AttributeName: "video_id",
                        KeyType: "RANGE"
                    }
                ],
                Projection: {
                    ProjectionType: "ALL"
                }
            }]

And after this I have inserted two items as below

Item 1:

{
    "company": "comapnyname",
    "created_date": "02-28-2019",
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe",
    "profile_pic": "https://s3location",
    "user_id": "vshdhfb"
  }

Item 2:

{
                "author": "Phil DiMaggio",
                "created_date": "02-29-2018",
                "description": "This video further introduces the Business Sales compensation plans.",
                "likes": "12",
                "title": "Understanding Business Sales Compensation Plans",
                "user_id": "vshdhfb",
                "video_id": "vdhk23so9",
                "video_loc": "https://s3.amazonaws.com/videos/video.mp4",
                "views": "45"
              }

And then I try to update/delete the second item with below code and it gives me an error as "message": "One of the required keys was not given a value"

var params = {
        TableName: "sample",

        Key:{
            "video_id": "vdhk23so9",
            "user_id":"vshdhfb"
        },
        UpdateExpression: "set likes = likes + :val",
        ExpressionAttributeValues:{
        ":val":1
        },
        ReturnValues:"UPDATED_NEW"
    };

    docClient.update(params, function(err, data) {
        if (err) {
            document.getElementById('textarea').innerHTML = "Unable to update like: " + "\n" + JSON.stringify(err, undefined, 2);
        } else {
            document.getElementById('textarea').innerHTML = "Like video succeeded: " + "\n" + JSON.stringify(data, undefined, 2);
        }
    });
} 

Do I need to add another key ? What am I missing ?

Upvotes: 0

Views: 8108

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269370

Your third code block is not providing a value for create_date. This field is defined as part of the key:

    TableName : "sample",
    KeySchema: [
        { AttributeName: "user_id", KeyType: "HASH"},
        { AttributeName: "created_date", KeyType: "RANGE"}

You must provide both user_id and created_date to uniquely identify a record.

Oh, and it's probably not causing a problem but this is an invalid date since 2018 is not a leap year:

"created_date": "02-29-2018",

The date is being stored as a string so it won't cause an error, but you might also consider storing dates in a more useful format, such as YYYY-MM-DD which makes it easier to sort and avoids confusion with US date formatting (eg is 01-02-2019 1-Feb or 2-Jan?).

Upvotes: 4

Related Questions