Home > Enterprise >  Delete the user by clicking the delete the button
Delete the user by clicking the delete the button

Time:01-05

I want to delete the user by clicking the delete button in frontend. so i want to delete the the user by _id : ObjectId('63b6bf098fbd152693f876e0') by this criteria. I had written the code of delete in express in backend but after testing through the postman it is not deleting. I tried other solution in stackoverflow but didn't got my solution

The id is giving undefined

Mycode

router.delete("/delete/:id", async (req, res) => {
  const movie = await User.findByIdAndDelete(req.body.id);
  console.log(req.body.id);

  if (!movie) {
    res.send("Movie not found");
  } else {
    movie.remove();
    res.send("movie deleted");
  }
});

Output i got in postman

Movie not found

Testing in postman through http://localhost:4000/delete/63b6becb8fbd152693f876d8

CodePudding user response:

You should access the req.params object for that. It will contain the id. See https://expressjs.com/en/guide/routing.html

CodePudding user response:

  1. It may be because =>

It depends on what are you sending in findByIdAndDelete(arg1)

Here arg1 takes the id in string format not the ObjectId.

  1. Also the movie may have been deleted once then you are receiving this message.
  • Related