how to update item in dynamoDB using nodejs?
Asked Answered
D

1

1

how do i update item in dynamoDB using nodejs ?

here is the ITEM list from DynamoDB javascript shell -

 "Items": [
        {
          "EmailId": "[email protected]",
          "flag": 1,
          "deviceOS": "IOS",
          "companyName": "VCC",
          "snsEndpoint": "00d0sadas",
          "CreatedAt": 22112015,
          "Otp": "ABCDEF",

        },

i want to update flag value to 2 ... this is my code . what do i do?? what am i doing wrong ?? help is appreciated...

var params = {
                TableName: 'users',
                Key: {
                    id: {
                        'S': req.query.id
                    },
                    flag: {
                        'N': 2
                    }
                },               
                UpdateExpression: 'SET #flag =:val1',
                ExpressionAttributeNames: {
                    '#flag': 'flag' //COLUMN NAME       
                },
                ExpressionAttributeValues: {
                    ':val1': {
                        'N': 2
                    },
                }
            };
            dynamodb.updateItem(params, function(err, data) {
                if (err) {
                    console.log('Error :' + err);
                } else {
                    //subscribe(bodydata.id);
                    console.log('EndpointArn Saved successful');
                    console.log('Data :' + JSON.stringify(data.flag));
                }
            });
Dissentient answered 22/11, 2015 at 18:27 Comment(0)
B
2

You are trying to modify the flag: { 'N': 2 } which doesnot exist. But you wanted to modify the flag: { 'N': 1 } value to 2. So try doing like this:

var params = {
                TableName: 'users',
                Key: {
                    id: {
                        'S': req.query.id
                    },
                    flag: {
                        'N': 1
                    }
                },               
                UpdateExpression: 'SET #flag =:val1',
                ExpressionAttributeNames: {
                    '#flag': 'flag' //COLUMN NAME       
                },
                ExpressionAttributeValues: {
                    ':val1': {
                        'N': 2
                    },
                }
            };
            dynamodb.updateItem(params, function(err, data) {
                if (err) {
                    console.log('Error :' + err);
                } else {
                    //subscribe(bodydata.id);
                    console.log('EndpointArn Saved successful');
                    console.log('Data :' + JSON.stringify(data.flag));
                }
            });
Bohs answered 23/11, 2015 at 7:22 Comment(1)
did you try this? how it's work flag is a inner attribute of Items how its know search data in Items listAbelmosk

© 2022 - 2024 — McMap. All rights reserved.