使用 NodeJS 更新 DynamoDB 中的项目时出现错误 Invalid UpdateExpression

when Updating items in DynamoDB giving error Invalid UpdateExpression using NodeJS

这是我的处理函数。

const updateOrder = (request) => {
    const id = request.pathParams.id;
    const orderObject = request.body;
    if(!id || !orderObject)
        throw new Error ('Please provide required id and object in order to update the order')


        const params = {
            TableName: "pizza-order",
            Key: {
                "orderId": id
            },
            UpdateExpression: "SET  #n = :n, #a = :a ",
            ExpressionAttributeNames: {
                "#n": "pizza",
                "#a": "address"
            },
            ConditionExpression: '' ,
            ExpressionAttributeValues: { 
                ":n": orderObject.pizza,
                ":a": orderObject.address,
            },
            
        };

        return docClient.update(params).promise()

}

订单对象是:

{
    "pizza":"THe outstanding pizza",
    "Address": "feni"
}

它 returns 这样的错误。我不知道为什么会这样。无服务器开发的新手。有人知道解决方案吗?

这是一个非常愚蠢的错误。首先在订单对象中有一个错字:

{
    "pizza":"THe outstanding pizza",
    "address": "feni" //previously it was Address: "feni
}

而第二个原因是ConditionExpression不能为空

const updateOrder = (request) => {
    const id = request.pathParams.id;
    const orderObject = request.body;
    if(!id || !orderObject)
        throw new Error ('Please provide required id and object in order to update the order')

        const params = {
            TableName: "pizza-order",
            Key: {
                "orderId": id
            },
            UpdateExpression: "SET  #n = :n, #a = :a ",
            ExpressionAttributeNames: {
                "#n": "pizza",
                "#a": "address"
            },
            ConditionExpression: '' , // this can not be empty
            ExpressionAttributeValues: {
                ":n": orderObject.pizza,
                ":a": orderObject.address,
            },
            
        };

        return docClient.update(params).promise()

}