【问题标题】:how to update item in dynamoDB using nodejs?如何使用 nodejs 更新 dynamoDB 中的项目?
【发布时间】:2016-02-24 19:37:10
【问题描述】:

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

这是来自 DynamoDB javascript shell 的项目列表 -

 "Items": [
        {
          "EmailId": "swa@acc.com",
          "flag": 1,
          "deviceOS": "IOS",
          "companyName": "VCC",
          "snsEndpoint": "00d0sadas",
          "CreatedAt": 22112015,
          "Otp": "ABCDEF",

        },

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

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

【问题讨论】:

    标签: javascript json node.js amazon-dynamodb dynamo-local


    【解决方案1】:

    您正在尝试修改不存在的flag: { 'N': 2 }。但是您想将 flag: { 'N': 1 } 的值修改为 2。所以尝试这样做:

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

    【讨论】:

    • 你试过了吗?它的工作方式标志是 Items 的内部属性,它如何知道 Items 列表中的搜索数据
    猜你喜欢
    • 2017-03-01
    • 2019-02-16
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多