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



这是我的处理程序函数。

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

它返回这样的错误。我不知道为什么会这样。对于无服务器开发来说是非常新的。有人知道解决办法吗?

这是一个非常愚蠢的错误。订单中的第一个对象有一个拼写错误:
{
"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()
}

最新更新