条件DynamoDB更新无法正常工作

时间:2017-07-13 20:31:24

标签: node.js amazon-web-services amazon-dynamodb aws-lambda

我正在尝试创建一个无声拍卖网站。我的Lambda函数应在提交的出价>条件下更新项目。当前的高出价。然而,这个条件似乎没有适用;即使提交的出价低于当前的高出价,也会进行更新。 请帮我确定一下我做错了什么:

##我的DynamoDB表如下所示:##

  • 表名:SilentAuction2
  • 主分区键:ItemID(String)
  • 其他属性:
    • CurrentHighBid(Number)
    • HighestBidder(String)
    • Notes(String)
    • BidHistoryBids(列表)
    • BidHistoryPPL(列表)


如上所述,我的条件是传入的event.NewBid大于CurrentHighBid。这是Node.JS中的Lambda代码:

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function index(event, context, callback) {
        var table = "SilentAuction2";

var ItemID = event.ItemID;
var NewBid = event.NewBid;
var NewBidder = event.NewBidder;
var Notes = event.Notes;

var params = {
    TableName:table,
    Key:{
        "ItemID": ItemID
},
  //update and append values      
UpdateExpression: "SET #BidList = list_append(#BidList, :hb),
                       #PplList = list_append(#PplList, :hp),
                       CurrentHighBid =:c, 
                       HighestBidder=:h, 
                       Notes=:n",
  //condition on which to update if true
ConditionalExpression: " :c > CurrentHighBid",

ExpressionAttributeNames: {
      "#BidList" : "BidHistoryBids",
      "#PplList" : "BidHistoryPPL"
  },

ExpressionAttributeValues:{
     ":hb":[NewBid],
     ":hp":[NewBidder],
     ":n":Notes,
     ":c":NewBid,
     ":h":NewBidder

        }, 
ReturnValues:"UPDATED_NEW"
    };

docClient.update(params, function(err, data){
     if(err){
          callback(err, null);
     }else{
          callback(null, data);
            }
     });
};

此链接有助于研究DynamoDB中的ConditionalExpression: https://egkatzioura.com/2016/08/09/update-dynamodb-items-with-node-js/

http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.05

0 个答案:

没有答案