React-即使API请求发布,获取也无法代理请求

时间:2020-05-07 12:47:14

标签: node.js reactjs express routes mern

我收到此错误;

代理错误:无法代理请求 / betinfo / edit / 5eb1d40c26201e00176decb0从localhost:3000到 http://localhost:5000

但是axios会发布内容,并且编辑内容会进入数据库。

这仅发生在我的一条路线中。

我的package.json中确实有“ proxy”:“ http://localhost:5000

路线

// edit bet by specific bet id
router.route('/edit/:id').post(async (req, res) => {

try {
const { event, selection, odds, stake, bookmaker } = req.body;

const response = await Bet.findByIdAndUpdate(req.params.id, { event, selection, odds, stake, bookmaker }, { new: false });

if (!response) return res.status(400).send("Data not found");


} catch (err) {
res.status(500).send("Something went wrong");
return;
}

});

动作

export const editBet = (id, betData) => dispatch => {
 axios
     .post("/betinfo/edit/" + id, betData)
     .catch(err =>
         dispatch({
             payload: err.response.data
         })
     );
};

错误的出现并不重要,因为它仍然会发布。但是,当我将其推送到heroku时,发布此路由时会收到“请求超时”。

当我按下按钮发布时,什么也没有发生。但是刷新/重新加载页面后,就会收到代理错误。

package.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.0",
    "classnames": "^2.2.6",
    "jwt-decode": "^2.2.0",
    "react": "^16.11.0",
    "react-dom": "^16.11.0",
    "react-redux": "^7.1.3",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.2.0",
    "redux": "^4.0.4",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

0 个答案:

没有答案