Reactjs 反应路由器Dom命中错误端点

Reactjs 反应路由器Dom命中错误端点,reactjs,express,react-redux,react-router,react-router-dom,Reactjs,Express,React Redux,React Router,React Router Dom,我有一个redux操作,每当站点上的用户试图更改他们的配置文件时就会触发。它是通过api调用中间件实现的,我使用Mosh的教程编写了该中间件 export const updateUserProfile = ({ id, name, email, favouriteThing, password, confirmPassword }, headers) => (dispatch) => { dispatch( apiCallBegan({

我有一个redux操作,每当站点上的用户试图更改他们的配置文件时就会触发。它是通过api调用中间件实现的,我使用Mosh的教程编写了该中间件

export const updateUserProfile =
  ({ id, name, email, favouriteThing, password, confirmPassword }, headers) =>
  (dispatch) => {
    dispatch(
      apiCallBegan({
        url: `api/users/profile`,
        data: { id, name, email, favouriteThing, password, confirmPassword },
        headers,
        method: 'put',
        onStart: userUpdateRequested.type,
        onSuccess: userUpdateReceived.type,
        onError: userUpdateFailed.type,
      })
    );
  };
应用程序路径如下所示:

          <Route path='/profile' render={ProfileScreen} exact />
          <Route path='/profile/page/:pageNumber' render={ProfileScreen} exact/>
  const submitHandler = (e) => {
    e.preventDefault();
    if (password !== confirmPassword) {
      setMessage('Passwords do not match');
    } else {
      dispatch(updateUserProfile(data, headers));
    }
  };
我使用提交处理程序提交新数据,如下所示:

          <Route path='/profile' render={ProfileScreen} exact />
          <Route path='/profile/page/:pageNumber' render={ProfileScreen} exact/>
  const submitHandler = (e) => {
    e.preventDefault();
    if (password !== confirmPassword) {
      setMessage('Passwords do not match');
    } else {
      dispatch(updateUserProfile(data, headers));
    }
  };
当我位于以下URL时,所有这些都可以正常工作:http://localhost:3000/profile

当我转到个人资料的第2页时,这是URL:http://localhost:3000/profile/page/2 我得到以下错误:

404尝试访问/profile/page/api/users/profile时未找到

触发submitHandler时,我记录了location.pathname,并获得了正确的“/profile”。 但是,当我在express中的错误中间件中记录req.originalURL时,我得到/profile/page/api/users/profile

因此,在前端调用函数和后端接收函数的方式之间,原始URL发生了变化

不管我在档案的哪一页上,是否仍要将req.originalURL保留在“/profile”上?我曾尝试在React Router Dom中使用Switch组件,并将配置文件页路由的位置设置为“/profile”,但这会中断配置文件页路由,并且当您单击下一页时,配置文件页根本不会加载


我在应用程序中做了很多谷歌搜索和测试,但我似乎想不出任何东西,这让我觉得我对这里发生的事情有一个根本性的误解。如果有人有任何想法,我真的很感谢你的帮助。

事实证明,有人在另一个网站上向我指出了这是一个非常简单的修复方法。我想我会在这里分享

请求中url的开头需要有一个正斜杠,否则会被解释为相对路径

url: `/api/users/profile`