-1

I am trying to delete an entry from json data.

This is what I do to view the data:

app.route('/data/:id')
    .get((req:Request, res: Response) => {
    let id = req.params.id;
    res.status(200).send(projects[id]);
});

And that will show the data with that id in the json data.

This is what I've got to delete:

app.route('/data/delete/:id')
    .delete((req:Request, res: Response) => {
    let id = req.params.id;
    res.status(200).send(projects[id]);
});

What I'm I doing wrong, missing from the delete code above?

  • Projects, is it a array? – Ajantha Bandara Jan 16 '19 at 13:34
  • Projects contain some json: { "1":{ "name":"data1", }, "2":{ "name":"data2", } } –  Jan 16 '19 at 13:39
  • 1
    Possible duplicate of [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – str Jan 16 '19 at 13:41

2 Answers2

1

Use delete keyword and return the response

delete projects[id]
Ajantha Bandara
  • 1,473
  • 15
  • 36
0

You've created a delete route containing the code which will run when an HTTP client asks your HTTP server to delete something matching the URL.

The code you've written inside it gets the id, then sends it from the projects object.

So what you are missing is code which actually deletes anything.


You can use the delete keyword to do this.

let id = req.params.id;
delete projects[id];

You probably shouldn't return data you just deleted though. You can send a 204 No Content response instead.

res.status(204).send()
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335