简体   繁体   English

React 中的 Axios/Backend 会在一段时间后停止工作且没有错误

[英]Axios/Backend in React stops working after a while with no error

My React App takes user input from frontend and send it to backend via Axios where the info will be used to search through MongoDB.我的 React 应用程序从前端获取用户输入并通过 Axios 将其发送到后端,该信息将用于搜索 MongoDB。 My App works fine for a few input, like around 5-6, then it will stop working with no error and I have no idea what is wrong and how to fix it.我的应用程序适用于一些输入,例如 5-6 左右,然后它将停止工作而没有错误,我不知道出了什么问题以及如何修复它。

Here is an example of my problem, this useEffect will happen when page load and take param from the url and send it to backend (the param change depends on what link the user clicks on).这是我的问题的一个示例,当页面加载并从 url 获取参数并将其发送到后端时,此 useEffect 将发生(参数更改取决于用户单击的链接)。 It will then send the information to backend via axios and the Info is used to search through MongoDB and will dynamically generate the page's text.然后它将信息通过 axios 发送到后端,Info 用于搜索 MongoDB 并将动态生成页面的文本。 After the user clicks on around 5 different links tho, this will stop working and the page generated will be stuck on the last page it was generated dynamically although the params in the url still change.在用户单击大约 5 个不同的链接后,这将停止工作,并且生成的页面将停留在动态生成的最后一页上,尽管 url 中的参数仍在更改。 I added console.log to see if useEffect was activating or not, and everytime I click on a Link, the console.log will work when the page load.我添加了 console.log 以查看 useEffect 是否激活,每次单击链接时,console.log 都会在页面加载时工作。 Any lead will help, thanks!任何线索都会有所帮助,谢谢!

frontend runs on port 3000 and backend runs on 3001前端运行在 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))}, [])

This is the backend这是后台

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

This way, when you fetch('/api/info') in development, the development server will recognize that it's not a static asset, and will proxy your request to http://localhost:3001/api/info as a fallback.这样,当您在开发中 fetch('/api/info') 时,开发服务器将识别它不是 static 资产,并将您的请求代理到 http://localhost:3001/api/info 作为后备。 The development server will only attempt to send requests without text/html in its Accept header to the proxy.开发服务器只会尝试将其 Accept header 中没有 text/html 的请求发送到代理。 Conveniently, this avoids CORS issues and error messages like this in development:方便的是,这避免了 CORS 问题和开发中这样的错误消息:

Fetch API cannot load http://localhost:3001/api/info.获取 API 无法加载 http://localhost:3001/api/info。 No 'Access-Control-Allow-Origin' header is present on the requested resource.请求的资源上不存在“Access-Control-Allow-Origin”header。 Origin 'http://localhost:3000' is therefore not allowed access.因此,不允许访问 Origin 'http://localhost:3000'。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

https://create-react-app.dev/docs/proxying-api-requests-in-development/ https://create-react-app.dev/docs/proxying-api-requests-in-development/

src/setupProxy.js src/setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:3001',
      changeOrigin: true,
    })
  );
};

useEffect(() => {
        const idInfo = {id};
        axios.post('/api/info', idInfo);
        console.log("testing")
    }, [])

useEffect(() => {
        fetch("/api/info").then(res => {
            if(res.ok) {
                return res.json();
                
            }
        }).then(jsonRes => setGetId(jsonRes))}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM