第一个问题,不知道要搜索什么才能找到解决方案,所以就这样吧。
我正在使用React、Mongo、Express、Node。
PatientDetails.js
这就是我在打开详细信息卡片时获取特定患者的方式。
useEffect(() => {
fetch(`/api/patients/${match.params.id}`)
.then(response => response.json())
.then(json => setPatient(json))
}, [patient])
这就是我如何使用onclick方法删除患者详细信息卡片上的评论。
const deleteNote = (noteID) => {
fetch(`/api/patients/${patient._id}/notes/${noteID}`, {
method: 'DELETE',
})
.then(response => response.json())
};
这是我的API路由。
// @route DELETE api/patients/:id/notes/:noteId
// @desc Delete a note
router.delete('/:id/notes/:noteId', async (req, res) => {
const {id, noteId} = req.params
await Patient.findByIdAndUpdate(id, {$pull: {notes: noteId}});
await Note.findByIdAndRemove(noteId);
})
我可以删除3-4个评论之前,页面只是结束加载永远,我得到没有错误的前端或后端,我必须重新启动我的后端服务器,然后我能够删除3-4个评论之前,这种情况再次发生。
我做错了什么?
发布于 2020-11-15 18:07:21
您在前端和后端都有一些问题:
在React组件中,您应该将match.params.id
添加为钩子的依赖项:
useEffect(() => {
fetch(`/api/patients/${match.params.id}`)
.then(response => response.json())
.then(json => setPatient(json))
}, [match.params.id])
在Express中间件中,您应该在修改数据库后发送响应:
router.delete('/:id/notes/:noteId', async (req, res) => {
const {id, noteId} = req.params
await Patient.findByIdAndUpdate(id, {$pull: {notes: noteId}});
await Note.findByIdAndRemove(noteId);
res.status(200).send({});
})
https://stackoverflow.com/questions/64842809
复制相似问题