Mongoose的Model.findByIdAndDelete()成功删除文档,但之后卡住了。

huangapple go评论59阅读模式
英文:

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

问题

这是我的控制器:

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 };

这是路由:
router.route("/quiz/:id").get(getQuizById).delete(removeQuizById);

英文:

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!

答案1

得分: 1

res.status(200)不会结束响应。

尝试使用res.sendStatus(200)

英文:

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

try res.sendStatus(200)

huangapple
  • 本文由 发表于 2023年7月18日 07:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定