简体   繁体   English

如何在NodeJs中的AWS DynamoDb中更新?

[英]How to update in AWS DynamoDb in NodeJs?

I have a code which is updating an existing AWS DynamoDb item, 我有一个代码正在更新现有的AWS DynamoDb项目,
I am using the update method of the AWS DynamoDb SDK, 我正在使用AWS DynamoDb SDK的更新方法,
I am getting an error while updating the item - 更新项目时出现错误-

{ ValidationException: ExpressionAttributeValues contains invalid key: Syntax error; key: "tamt"
message: 'ExpressionAttributeValues contains invalid key: Syntax error; key: "tamt"',
  code: 'ValidationException',
  statusCode: 400,
  retryable: false,

Following is my code - 以下是我的代码-

module.exports.updateCart = async (constants, connection, requestBody) => {
    let table = constants.tables.user_cart;

    let params = {
        TableName: table,
        Key:{
            "cart_id": requestBody.cart_id
        },
        UpdateExpression: "set currency = :curr, product_type = :ptype, total_amount = :tamt,"
        +"total_quantity = :tqty, created_date = :cdate, expiry = :exp, items = :items",
        ExpressionAttributeValues:{
            "curr": requestBody.currency,
            "ptype": requestBody.product_type,
            "tamt": requestBody.total_amount,
            "tqty": requestBody.total_quantity,
            "cdate": requestBody.created_date,
            "exp": requestBody.expiry,
            "items" : requestBody.items
        },
        ReturnValues:"UPDATED_NEW"
    };
    console.log("Params => \n", params);

    let update = util.promisify(connection.update).bind(connection);
    let results = await update(params);
    return results;
}

Following is the console log of Params - 以下是Params的控制台日志-

{
    TableName: 'someTable',
    Key: {
        cart_id: 'someId'
    },
    UpdateExpression: 'set currency = :curr, product_type = :ptype, total_amount = :tamt, total_quantity = :tqty, created_date = :cdate, expiry = :exp, items = :items',
    ExpressionAttributeValues: {
        curr: 'USD',
        ptype: 'SPECIAL',
        tamt: 50,
        tqty: 1,
        cdate: 1558499016,
        exp: 1558499016,
        items: items: [{
            name: 'someName',
            amount: 50,
            quantity: 1,
            category: 'some Category'
        }]
    },
    ReturnValues: 'UPDATED_NEW'
}

RequestBody - RequestBody-

{
    product_type: 'SPECIAL',
    user_id: 1234,
    total_amount: 50,
    total_quantity: 1,
    currency: 'USD',
    items: [{
        name: 'someName',
        amount: 50,
        quantity: 1,
        category: 'some Category'
    }],
    created_date: 1558499016,
    expiry: 1558499016,
    cart_id: 'someId'
}

I double checked, total_amount field on the table is of Number type, 我仔细检查了一下,表上的total_amount字段是数字类型,
What could be the issue? 可能是什么问题?

Ref - 参考-
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.03 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.03

the keys in ExpressionAttributeValues should start with a : ExpressionAttributeValues的键应以开头:

ExpressionAttributeValues:{
        ":curr": requestBody.currency,
        ":ptype": requestBody.product_type,
        ":tamt": requestBody.total_amount,
        ":tqty": requestBody.total_quantity,
        ":cdate": requestBody.created_date,
        ":exp": requestBody.expiry,
        ":items" : requestBody.items
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM