我正在尝试使用Lambda和API网关从DynamoDB删除记录。当前,记录的ID作为查询参数传递,传递给我的deleteRecord lambda函数,并在documentClient的delete函数中使用。
当前,我在尝试删除记录时收到错误消息,但该记录还是被删除了,我不知道为什么。
在API网关上进行测试时,我收到“内部服务器错误”作为响应,但检查表显示该项目已删除,日志显示了已删除的记录(按提供返回值:传递给documentClient的 params 中的“ ALL_OLD” 。
直接在Lambda上测试时(手动将ID插入我的 dt.delete 函数中),我得到以下信息:
Response:
{
"errorMessage": "RequestId: xxxxxxxxxxxxxxxxxxxx Process exited before completing request"
}
Request ID:
"xxxxxxxxxxxxxxxxxxxx"
Function Logs:
START RequestId: xxxxxxxxxxxxxxxxxxxx Version: $LATEST
2019-04-04T10:21:55.637Z xxxxxxxxxxxxxxxxxxxx Promise { <pending> }
2019-04-04T10:21:55.938Z xxxxxxxxxxxxxxxxxxxx TypeError: undefined is not a function
at Response.<anonymous> (/var/task/deleteTrip.js:28:28)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:364:18)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)
END RequestId: xxxxxxxxxxxxxxxxxxxx
REPORT RequestId: xxxxxxxxxxxxxxxxxxxx Duration: 941.48 ms Billed Duration: 1000 ms Memory Size: 128 MB Max Memory Used: 67 MB
RequestId: xxxxxxxxxxxxxxxxxxxx Process exited before completing request
尽管有错误,在这种情况下,记录也会从表中删除。
我已经按照DocumentClient documentation删除了一条记录,但是它没有说明如何传递回响应(而是只是记录回叫中的数据),所以我不确定这是否是我的问题
Lambda函数
Index.js:
const deleteTrip = require('./deleteTrip.js');
const dt = new deleteTrip();
exports.handler = async (event) => {
return dt.delete(event.queryStringParameters['Id']);
};
deleteTrip.js:
const AWS = require('aws-sdk');
let documentClient = new AWS.DynamoDB.DocumentClient({
'region': 'eu-west-1'
});
module.exports = class deleteTrip {
delete(Id) {
return new Promise((resolve, reject) => {
if (typeof Id !== 'string') throw `Expected: "String", received: ${typeof Id}`;
let params = {
TableName: "xyzDataTable",
Key: {
tripId: Id
},
ReturnValues: "ALL_OLD",
Exists: true
};
documentClient.delete(params, function(err, data) {
if (err) {
console.log("Error deleting record from dynamoDB:", err);
console.log(params);
return reject(err);
} else {
return resolve(...data.Attributes);
}
})
});
}
}
我希望如果请求成功,将返回已删除项目的详细信息,否则,将引发错误。
答案 0 :(得分:0)
在Lambda中,导出的处理程序返回值后,该函数将立即退出。您正在返回承诺,甚至还没有解决,过程就已经退出。
在index.js中进行以下更改
const deleteTrip = require('./deleteTrip.js');
const dt = new deleteTrip();
exports.handler = async (event) => {
try{
var result = await dt.delete(event.queryStringParameters['Id']);
return res;
}
catch(err){
//if the delete function throws an error or the promise rejects
return err;
}
};
答案 1 :(得分:0)
这是一个如何在DynamoDB DocumentClient(或者实际上也是较低级别的DynamoDB API)中使用Promise的示例:
const AWS = require('aws-sdk');
let documentClient = new AWS.DynamoDB.DocumentClient({
'region': 'eu-west-1'
});
class deleteTrip {
delete(Id) {
const params = {
TableName: "xyzDataTable",
Key: {
tripId: Id
},
ReturnValues: "ALL_OLD",
Exists: true
};
return documentClient.delete(params).promise();
}
}
const testing = async (id) => {
try {
const dt = new deleteTrip();
const data = await dt.delete(id);
console.log("Success:", data);
} catch(err) {
console.log("Error:", err);
}
}
testing(101);
还请注意,如果您尝试删除不存在的项目,则删除操作将成功,即不会报告任何错误。这被认为是成功的,而这只是DynamoDB的本质。