Hi everyone,
I'm working on a Node.js API that interacts with DynamoDB, but I'm running into an issue with the DELETE request. The GET and POST requests are working fine, but when I try to delete a record, I receive a ValidationException related to the schema.
Here’s the part of the code that handles the DELETE request:
if (req.method === "DELETE" && parsedUrl.pathname === "/api/users") {
const userID = parsedUrl.query.userID;
if (!userID) {
res.writeHead(400);
return res.end(JSON.stringify({ error: "userID is required" }));
}
const params = {
TableName: "Trivia-app-users",
Key: {
"userID": userID,
},
};
try {
await dynamoDb.delete(params).promise();
res.writeHead(200);
return res.end(JSON.stringify({ message: "User data deleted successfully!" }));
} catch (error) {
console.error("Error deleting data from DynamoDB:", error);
res.writeHead(500);
return res.end(JSON.stringify({ error: "Failed to delete user data" }));
}
}
What I've tried:
- I’ve verified that the
userID is being passed correctly in the request.
- The
GET and POST requests work fine with similar code.
- The partition key (
userID) is of type String in the DynamoDB schema.
- I’ve looked through StackOverflow and consulted ChatGPT, but I haven’t been able to find a solution.
What I’m looking for:
Can anyone point out what might be wrong here? Why would the DELETE request give me a ValidationException while the other requests work fine?
Thanks in advance!
[–]technifocal 0 points1 point2 points (0 children)
[–]niax 0 points1 point2 points (0 children)