Why does Delete method of Amazon DynamoDB not return deleted result?

0

Issue

I am creating an express api for delete operation, after successful deletion of item, i want that deleted item to be shown as response, the item is getting deleted from the table but the API gives empty object {} as response when ran from CURL command, i am following these docs from aws.

here is my code

router.delete('/delete/:id',function(req, res, next) {
  
  const params = {
    TableName: 'Notes',
    Key: {
      id: req.params.id,
    },
  };

  dynamoDb.delete(params, function (error,data) {
    if (error) {
      console.log(error);
      res.status(400).json({ error: 'Could not update user' });
    }
    res.status(200).json(data);
  });
})

curl command

curl -H "Content-Type: application/json" -X DELETE xxxxxxxxxxxxxxx/dev/user/delete/alexdebrie2 

Solution

The DynamoDB documentation of the DeleteItem operation clearly explains that:

In addition to deleting an item, you can also return the item’s attribute values in the same operation, using the ReturnValues parameter.

So a delete operation does not, by default, return the old value of the deleted item. If you want that, you need to pass the ReturnValues option to the DynamoDB request. I’m not familiar with whatever programming language you’re using to implement this, but I hope you can figure this out for yourself.

Answered By – Nadav Har'El

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More