英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论