Search code examples
mongoosepostmanrouterexpress-routermongoose-deleteone

Mongoose's Model.findByIdAndDelete() successfully deletes document but hangs afterwards


I am using express with mongoose and MongoDB. After creating a simple deletion route for my "Quiz" model, it deletes the document, however it never actually receives a response. I have tested with Postman and Insomnia.

Here is my controller:


const removeQuizById = async (req, res, next) => {
  try {
    const { id } = req.params;
    quiz = await Quizz.findById(id);
    if (quiz) {
      await Quizz.findByIdAndDelete(id);
      res.status(200);
    } else {
      res.status(404);
      throw new Error("quiz does not exist");
    }
  } catch (error) {
    return next(error);
  }
};

module.exports = { getQuizById, removeQuizById };

This is the route: router.route("/quiz/:id").get(getQuizById).delete(removeQuizById);

Here is a response of a GET, so you can see the quiz does exist: return of Get in postman

Here is what happens when using the delete route: Delete in postman

And here is after canceling and sending one more time, to confirm the document was indeed deleted:Response after sending one more time

Thank you for your help!


Solution

  • res.status(200) does not end the response.

    try res.sendStatus(200)