尽管Lambda更新函数成功返回DynamoDB,但仍未更新

时间:2019-02-23 17:59:58

标签: javascript node.js aws-lambda amazon-dynamodb

由于某种原因,我的Lambda函数似乎可以正常工作,但是无法更新数据库中的条目。这是我的Lambda函数,我已经确认它通过console.logs接收了正确的数据。对于GET / POST / DELETE操作,我具有一些类似的功能,这些功能可以正常工作,因此我知道数据库连接在那里。由于某种原因,它无法更新此列。

const AWS = require("aws-sdk");
const client = new AWS.DynamoDB.DocumentClient();

module.exports.run = async (event) => {

let category = event.category
let draggedRev = event.draggedRev
let targetRev = event.targetRev

let tmpDrag = Object.assign({}, draggedRev)

if (category == 'topfive') {

    var params = {
        TableName: 'reviews',
        Key:{
            "id": draggedRev.id
        },
        UpdateExpression: "SET #attrName = :attrValue",
        ExpressionAttributeNames: {
            "#attrName" : "topfive"
        },
        ExpressionAttributeValues:{
            ":attrValue": {
                "S": (parseInt(targetRev.topfive) + 5).toString()
            } 
        }
    };

    console.log("Updating the item...");
   await client.update(params, function(err, data) {
        if (err) {
            console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
        }
    })

}

return {
  statusCode: 200,
  headers: {
    'Access-Control-Allow-Origin':'*',
    'Access-Control-Allow-Headers': 'Content-Type,x-requested-with,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Access-Control-Allow-Methods',
    'Access-Control-Allow-Methods': 'PUT'
  },
  body: JSON.stringify(event)
};

};

1 个答案:

答案 0 :(得分:0)

您应该尝试通过在.promise()方法上调用DocumentClient来等待诺言。

try {
    const data = await client.update(params).promise();
    console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
} catch (error) {
    console.error("Unable to update item. Error JSON:", JSON.stringify(error, null, 2));
}

此外,您是否尝试不向ExpressionAttributeValues提供类型?

ExpressionAttributeValues:{
    ":attrValue": (parseInt(targetRev.topfive) + 5).toString()
}