Amit Shakya
Amit Shakya

Reputation: 994

how make update query in Aws DynamoDB using nodeJS

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.

Upvotes: 2

Views: 3395

Answers (2)

Chhin Vanchhai
Chhin Vanchhai

Reputation: 1

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}
  }

Upvotes: 0

Amit Shakya
Amit Shakya

Reputation: 994

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.

Upvotes: 4

Related Questions