【发布时间】:2020-09-12 01:51:20
【问题描述】:
我正在尝试编写一个 Lambda 函数来从 DynamoDB 的列表中删除一个对象。在这种情况下,我将"author": "J.K. Rowling"、"title": "Harry Potter" 和"userid": "041c9004" 作为参数传入。我想从books 列表中删除匹配的对象。 UpdateExpression 语句的正确语法是什么? params{} 中可能还有其他一些错误。
DynamoDB 表如下所示。 userid 是主键:
{
"books": [
{
"author": "J.R.R. Tolkien",
"title": "Lord of the Rings"
},
{
"author": "J.K Rowling",
"title": "Harry Potter"
},
{
"author": "George RR Martin",
"title": "A Song of Ice and Fire"
}
],
"isactive": true,
"ispublic": true,
"lastupdated": 1597690265,
"userid": "041c9004"
}
这是 Lambda 函数:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
exports.handler = function(event, context, callback){
let params = {
ExpressionAttributeValues: {
":attrValue": [{
"author": event.author,
"title": event.title
}]
},
ExpressionAttributeNames : {
"#books" : "books"
},
Key: {
userid: event.userid
},
TableName: 'Users',
UpdateExpression: "REMOVE #books[:attrValue]", //this is incorrect
ReturnValues:"ALL_NEW",
};
docClient.update(params, function(err,data){
if(err) {
callback(err, null)
}else{
callback(null, data)
}
});
}
【问题讨论】:
标签: node.js amazon-web-services aws-lambda amazon-dynamodb