Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Mongo删除时后端崩溃,无错误_Node.js_Reactjs_Mongodb_Express_React Hooks - Fatal编程技术网

Node.js 从Mongo删除时后端崩溃,无错误

Node.js 从Mongo删除时后端崩溃,无错误,node.js,reactjs,mongodb,express,react-hooks,Node.js,Reactjs,Mongodb,Express,React Hooks,有史以来的第一个问题是,不知道要搜索什么来找到解决方案,所以你来了 我用的是React,Mongo,Express,Node PatientDetails.js 这是我在打开详细信息卡时获取特定患者的方式 useEffect(() => { fetch(`/api/patients/${match.params.id}`) .then(response => response.json()) .then(json => setPatient(json))

有史以来的第一个问题是,不知道要搜索什么来找到解决方案,所以你来了

我用的是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条评论


我做错了什么?

前端和后端都有一些问题:

在React组件中,您应该添加
match.params.id
作为钩子的依赖项:

useffect(()=>{
获取(`/api/patients/${match.params.id}`)
.then(response=>response.json())
.然后(json=>setPatient(json))
},[match.params.id])
在Express中间件中,修改数据库后应发送响应:

router.delete('/:id/notes/:noteId',异步(req,res)=>{
常量{id,noteId}=req.params
等待Patient.findByIdAndUpdate(id,{$pull:{notes:noteId}});
等待Note.findByIdAndRemove(noteId);
res.status(200).send({});
})

我觉得-在安装组件时,获取患者详细信息将是一次性事件。因此,您不需要将
patient
作为
useffect
的依赖项,您是否从后端获得超时,然后您必须重新启动服务器?DevTools未能加载SourceMap:无法加载内容:由于加载超时而取消加载在我尝试将[patients]作为依赖项删除后会发生同样的情况,但一般来说,我没有收到任何错误,后端只是停止发送数据,或前端停止抓取,两者之一。嘿,这似乎已经解决了我的问题。问题在于修改数据库后没有发送状态200,match.params.id不相关,删除时没有更新我的组件。它与express中间件解决方案完美配合。非常感谢你!不客气!:)请注意,钩子中match.params.id的使用与删除后更新组件无关。当url更改时,它会生成正在再次执行的钩子。如果要在删除请求完成时执行某些操作,则应在
deleteNote
承诺得到解决后添加该逻辑。