how to update item in dynamoDB using nodejs?

心不动则不痛 提交于 2019-12-24 14:01:56

问题


how do i update item in dynamoDB using nodejs ?

here is the ITEM list from DynamoDB javascript shell -

 "Items": [
        {
          "EmailId": "swa@acc.com",
          "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));
                }
            });

回答1:


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


来源:https://stackoverflow.com/questions/33858475/how-to-update-item-in-dynamodb-using-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!