如何使用nodejs更新dynamoDB中的项目? [英] how to update item in dynamoDB using nodejs?

查看:306
本文介绍了如何使用nodejs更新dynamoDB中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用nodejs更新dynamoDB中的项目?

how do i update item in dynamoDB using nodejs ?

这是DynamoDB javascript shell中的项目列表-

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",

        },

我要更新标志值等于2 ...这是我的代码。我该怎么办??我究竟做错了什么 ??感谢帮助...

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


推荐答案

您正在尝试修改 flag:{'N':2} 不存在。但是您想将标志:{'N':1} 的值修改为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));
                }
            });

这篇关于如何使用nodejs更新dynamoDB中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆