Home > Enterprise >  Axios delete method with Lambda
Axios delete method with Lambda

Time:01-05

I have the following code to delete a contact from my contactApp, which is using axios in React to send the request:

export const removeContact = createAsyncThunk(
  'contactsApp/contacts/removeContact',
  async (contactId, { dispatch, getState }) => {
    await axios.delete('https://api.com/prod', { 
      key1: `${contactId}`
     });
    console.log(">>>>>>>"   contactId)
    return contactId;
  }
);

and this is the Lambda function to delete the item from the DynamoDB table:

var params = {
  TableName : 'Contacts',
  Key: {
    id: event.key1
  },
};

var documentClient = new AWS.DynamoDB.DocumentClient({region: "us-west-2"});

documentClient.delete(params, function(err, data) {
  if (err) console.log(err);
  else console.log(data);
});

The problem is, the item won't remove from the table, and the reason is that Lambda not receiving the "contactId" from ReactApp, which is containing which id should be deleted. Because when I test the code from Lambda like the following, it works:

  Key: {
    id: "123" //Actual ID in table
  },

So I think the issue is this in react:

key1: `${contactId}`

What am I missing?

CodePudding user response:

DELETE requests do not have body.

use

await axios.delete('https://api.com/prod', { 
  data: {key1: `${contactId}`}
 });
  •  Tags:  
  • Related