我无法使用 DynamoDB BatchWrite 删除多个项目,包括主键和排序键
I Cant delete multiple items using DynamoDB BatchWrite including a primary key and sort key
所以我正在尝试删除同一用户的两条记录。但是,当我 运行 下面的 Lambda 函数时,它实际上不会控制台记录成功或失败,并且不会在 DynamoDB 中删除记录。任何关于如何修改以下内容以获得删除两条记录的预期结果的任何建议都将不胜感激。谢谢
- 包括几个用于故障排除的控制台日志
- PK和SK都是DynamoDB中的字符串
- 两个删除请求都是针对同一个用户的,只是同一个 table
中的两个不同条目
'use strict'
const AWS = require('aws-sdk');
AWS.config.update({ region: "us-east-1" });
exports.handler = async (event, context) => {
var ddb = new AWS.DynamoDB.DocumentClient();
var records = [{
DeleteRequest : {
Key : {
'PK' : 'username',
'SK' : 'C297329360'
}
},
DeleteRequest : {
Key : {
'PK' : 'username',
'SK' : 'R297329360'
}
}
}];
var params = {
RequestItems : {
'userTable' : records
}
};
console.log(JSON.stringify(records));
console.log(records);
console.log(JSON.stringify(params));
console.log(params);
ddb.batchWrite(params, function(err, data) {
if (err) {
console.log('Batch delete unsuccessful ...');
console.log(err, err.stack); // an error occurred
} else {
console.log('Batch delete successful ...');
console.log(data); // successful response
}
});
}
Test Event Name
DeleteReportById
Response
null
Function Logs
START RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Version: $LATEST
2022-02-11T13:49:56.007Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO [{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]
2022-02-11T13:49:56.046Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO [ { DeleteRequest: { Key: [Object] } } ]
2022-02-11T13:49:56.047Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO {"RequestItems":{"FitTool_Prod":[{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]}}
2022-02-11T13:49:56.065Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO { RequestItems: { userTable: [ [Object] ] } }
END RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
REPORT RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Duration: 589.81 ms Billed Duration: 590 ms Memory Size: 128 MB Max Memory Used: 77 MB Init Duration: 474.35 ms
Request ID
6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
您没有等待 batchWrite 调用完成,lambda 在实际将请求发送到 DynamoDB 之前终止。你的处理程序是异步的,所以你可以这样做:
await ddb.batchWrite(...).promise()
所以我正在尝试删除同一用户的两条记录。但是,当我 运行 下面的 Lambda 函数时,它实际上不会控制台记录成功或失败,并且不会在 DynamoDB 中删除记录。任何关于如何修改以下内容以获得删除两条记录的预期结果的任何建议都将不胜感激。谢谢
- 包括几个用于故障排除的控制台日志
- PK和SK都是DynamoDB中的字符串
- 两个删除请求都是针对同一个用户的,只是同一个 table 中的两个不同条目
'use strict'
const AWS = require('aws-sdk');
AWS.config.update({ region: "us-east-1" });
exports.handler = async (event, context) => {
var ddb = new AWS.DynamoDB.DocumentClient();
var records = [{
DeleteRequest : {
Key : {
'PK' : 'username',
'SK' : 'C297329360'
}
},
DeleteRequest : {
Key : {
'PK' : 'username',
'SK' : 'R297329360'
}
}
}];
var params = {
RequestItems : {
'userTable' : records
}
};
console.log(JSON.stringify(records));
console.log(records);
console.log(JSON.stringify(params));
console.log(params);
ddb.batchWrite(params, function(err, data) {
if (err) {
console.log('Batch delete unsuccessful ...');
console.log(err, err.stack); // an error occurred
} else {
console.log('Batch delete successful ...');
console.log(data); // successful response
}
});
}
Test Event Name
DeleteReportById
Response
null
Function Logs
START RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Version: $LATEST
2022-02-11T13:49:56.007Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO [{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]
2022-02-11T13:49:56.046Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO [ { DeleteRequest: { Key: [Object] } } ]
2022-02-11T13:49:56.047Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO {"RequestItems":{"FitTool_Prod":[{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]}}
2022-02-11T13:49:56.065Z 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 INFO { RequestItems: { userTable: [ [Object] ] } }
END RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
REPORT RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Duration: 589.81 ms Billed Duration: 590 ms Memory Size: 128 MB Max Memory Used: 77 MB Init Duration: 474.35 ms
Request ID
6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
您没有等待 batchWrite 调用完成,lambda 在实际将请求发送到 DynamoDB 之前终止。你的处理程序是异步的,所以你可以这样做:
await ddb.batchWrite(...).promise()