Piotr Kaliński
Piotr Kaliński

Reputation: 227

AWS Lambda, DynamoDB unable to delete object

this is my lambda function

var AWS = require("aws-sdk");
var dynamodb = new AWS.DynamoDB();
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {

var params = {
    TableName: 'xx',
    Key: {
        project_id : event.id,
        name: event.name
    }
};

docClient.delete(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
         callback(null, data);
     }}
 );
};

my test event is

{
"id": "1490022172442",
"name":"xcv"
}

And still i got error "The provided key element does not match the schema". POST and GET is working nice but I am stucked here.

Upvotes: 1

Views: 1960

Answers (2)

MelwinPhilip
MelwinPhilip

Reputation: 15

Below given is a code snippet for deleting an item where there is only primary key and no sort key is provided

ar AWS = require("aws-sdk");
var dynamodb = new AWS.DynamoDB();
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context) => {
console.log('event= ' + JSON.stringify(event));
console.log('context= ' + JSON.stringify(context));

var params = {
TableName: 'TableName',
Key: {
    kundeId : event.kundeId,
    name: event.name
}
};

docClient.delete(params, function(err, data) {
if (err) {
    console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
     console.log("DEBUG:  deleteItem worked. ");
    context.succeed(data);

 }}
 );
 };

Upvotes: 0

notionquest
notionquest

Reputation: 39186

In order to delete an item from DynamoDB table, you have to provide both partition and sort key in key attributes. It should work if you include sort key on key attributes.

Upvotes: 1

Related Questions