这里是ConditionExpression
的定义。
为使条件更新 成功必须满足的条件。
看看下面的条件表达式。首先,在表中的项具有以下值: -
UsageCount
= 1
EntitilementCount
= 5
局部变量:askedCount
= 1(即递增1的值)
var params = {
TableName: "tableName",
Key: {
"ID": '3'
},
UpdateExpression: "SET UsageCount = UsageCount + :askedCount",
ConditionExpression : "UsageCount < EntitilementCount",
ExpressionAttributeValues: {
":askedCount": askedCount,
},
ReturnValues: "UPDATED_NEW"
};
我已经成功地执行了4次程序。第五次,我收到了The conditional request failed
错误消息。这可以满足您的要求。
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":2}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":3}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":4}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
UpdateItem succeeded: {"Attributes":{"UsageCount":5}}
prompt>node updateitem_stock_conditionally.js
Updating the item...
Unable to update item. Error JSON: {
"message": "The conditional request failed",
"code": "ConditionalCheckFailedException",
"time": "2017-04-26T20:29:21.710Z",
"requestId": "d73571f3-17f3-4aca-a3cf-7f13019a0292",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
这种情况对所有情况都没有帮助。在你的例子中,如果:requestedCount = 3。如果你执行两次,那么它也允许第二次。为了防止这种情况,我们需要以下条件表达式 - (UsageCount + ** askedCount **)