【发布时间】:2021-05-23 20:32:56
【问题描述】:
我的 React 应用程序从前端获取用户输入并通过 Axios 将其发送到后端,该信息将用于搜索 MongoDB。我的应用程序可以很好地处理一些输入,例如 5-6 左右,然后它将停止工作而没有错误,我不知道出了什么问题以及如何解决它。
这是我的问题的一个示例,当页面加载并从 url 获取参数并将其发送到后端时会发生这种 useEffect (参数更改取决于用户单击的链接)。然后它将信息通过 axios 发送到后端,Info 用于在 MongoDB 中搜索并动态生成页面的文本。在用户单击大约 5 个不同的链接后,这将停止工作,并且生成的页面将卡在动态生成的最后一页上,尽管 url 中的参数仍然会发生变化。我添加了 console.log 以查看 useEffect 是否激活,每次单击链接时,console.log 都会在页面加载时工作。任何线索都会有所帮助,谢谢!
前端运行在 3000 端口,后端运行在 3001
useEffect(() => {
const idInfo = {id};
axios.post('http://localhost:3001/info', idInfo);
console.log("testing")
}, [])
useEffect(() => {
fetch("/info").then(res => {
if(res.ok) {
return res.json();
}
}).then(jsonRes => setGetId(jsonRes))}, [])
这是后台
router.route("/info").post((req, res) => {
console.log(req.body)
const idInfo = req.body.id;
console.log(idInfo);
current_id = idInfo;
})
router.route("/info").get((req,res) => {
Disease.find({_id: current_id})
.then(foundId => res.json(foundId))
})
【问题讨论】:
标签: javascript reactjs mongodb axios