2

I need to update one item of DynamoDB, here is my Code.

const Aws = require('aws-sdk');
const endPoint= 'http://dynamodb.us-east-2.amazonaws.com';
const IAm_user_Key=process.env.IAM_USER_KEY;
const IAm_user_Secret=process.env.IAM_USER_SECRET;

Aws.config.update({ accessKeyId:IAm_user_Key,secretAccessKey:IAm_user_Secret,region:'us-east-2',endpoint:endPoint });

var docClient = new Aws.DynamoDB.DocumentClient();
var table = "cards";

var params={
        TableName:table,
        key:{ "cardid":clientData.data.cardid},
        AttributeValue:{"data":clientData.data}
  }

docClient.update(params, function(err, data) {
            if (err) {
                console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
            } else {
                console.log("Added item:", JSON.stringify(data, null, 2));
            }
        })

I am facing issues while running this code here my error.

Unable to update item. Error JSON: {
"message": "Missing required key 'Key' in params", "code": "MissingRequiredParameter",
"time": "2019-07-20T07:47:06.490Z" }

But you can see I have Key object in params but still struggling for this.

2
  • If you want to put, you should use docClient.update instead of docClient.put. Refer to docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/… for more information. It looks like you're passing update params to the put method. Commented Jul 20, 2019 at 21:29
  • @Armin Sorry my mistake, I edit my question I need to docClient.update instead of put Commented Jul 21, 2019 at 9:42

2 Answers 2

4

I read docs too many times but there is a silly mistake, I found it is just typo error "key" in params should be "Key" K in uppercase only and this is working.

Sign up to request clarification or add additional context in comments.

2 Comments

I made a same mistake :D
I have the same issue, but my Key is in caps :(
0

The problem is in Key, You need to use primary key to find which row will you update in Table Like SQL such as Primary Key. IN DynamoDB Primary Key = PK+SK example if your: PK = 'cardid' AND SK = 'name'

var params={
        TableName:table,
        Key:{ "cardid":clientData.data.cardid, 'name': req.body.name},
        AttributeValue:{"data":clientData.data}
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.