lambda 函数返回 null 以删除 DynamoDB 中的项目
lambda function returning null for deleting item in DynamoDB
嗨,我一直在尝试让我的 lambda 函数删除 dynamo 数据库中的一个项目,但该函数只是返回 null,我什至不知道如何开始调试它,希望这里的人有知识来帮助
my table 将 guid 作为其主分区键,并将用户名作为其排序键
这是我的 .js 代码
const AWS = require("aws-sdk");
// Initialising the DynamoDB SDK
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const { guid, username } = event
const params = {
TableName: "Items", // The name of your DynamoDB table
Key:{
"guid": {"S" : guid},
"username": {"S" : username}
}
};
try {
// Utilising the scan method to get all items in the table
documentClient.delete(params, function(err, data) {
if (err) {
return("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
return("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});
}
catch (e) {
return {
statusCode: 500,
body: e
};
}
};
这是我在 lambda 中使用的测试事件的有效载荷
{
"guid": "34",
"username": "newusername"
}
您正在使用 async function handler。所以你的函数可能在你的代码真正有机会执行之前就完成了。
如 docs
中所示,您可以通过围绕 new Promise
包装代码来解决此问题
嗨,我一直在尝试让我的 lambda 函数删除 dynamo 数据库中的一个项目,但该函数只是返回 null,我什至不知道如何开始调试它,希望这里的人有知识来帮助
my table 将 guid 作为其主分区键,并将用户名作为其排序键
这是我的 .js 代码
const AWS = require("aws-sdk");
// Initialising the DynamoDB SDK
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const { guid, username } = event
const params = {
TableName: "Items", // The name of your DynamoDB table
Key:{
"guid": {"S" : guid},
"username": {"S" : username}
}
};
try {
// Utilising the scan method to get all items in the table
documentClient.delete(params, function(err, data) {
if (err) {
return("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
return("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});
}
catch (e) {
return {
statusCode: 500,
body: e
};
}
};
这是我在 lambda 中使用的测试事件的有效载荷
{
"guid": "34",
"username": "newusername"
}
您正在使用 async function handler。所以你的函数可能在你的代码真正有机会执行之前就完成了。
如 docs
中所示,您可以通过围绕new Promise
包装代码来解决此问题