TypeError:无法读取未定义的属性(Reading' PathName')错误

发布于 2025-01-28 06:05:40 字数 2179 浏览 1 评论 0原文

我正在尝试将路线从一个组件传递到另一个组件,并且遇到了这个错误。

TypeError:无法读取未定义的属性(读取'PathName')

我不知道为什么要遇到此错误。

这是我的用户访问

const useRouting = () => {
    const { primaryRole } = useAuth();
    const { asapUser } = useAsapContext();
    const [initialRoute, setInitialRoute] = useState(null);

    const routesMetadataForRole = useMemo(() => getRoutesForRole(primaryRole), [primaryRole]);

    const routes = useMemo(() => routesPerRole(routesMetadataForRole), [routesMetadataForRole]);

    useEffect(() => {
        if (primaryRole === 'asap-dept-member') {
            apiService.ApplicationService.getMemberApplication(asapUser.id).then(response => {
                const application = response.find(application => application.applicant.user.id === asapUser?.id);
                if (application !== null) {
                    setInitialRoute(routesMetadataForRole[2]?.path.replace(':id', application?.id));
                } else {
                    setInitialRoute(routesMetadataForRole[0]?.path);
                }
            });
        } else {
            setInitialRoute(routesMetadataForRole[0]?.path);
        }
    }, [primaryRole, routesMetadataForRole, asapUser.id]);

    return { routes, routesMetadataForRole, initialRoute };
};

,这是我的baselayoutretes

const BaseLayoutRoutes = () => {
    const navigate = useNavigate();
    const { routes, initialRoute } = useRouting();
    const location = useLocation();

    useEffect(() => {
        if (location.pathname === '/' && routes && routes.length > 0) {
            navigate(initialRoute, { replace: true });
        }
    }, [initialRoute, navigate, location.pathname, routes]);

    return <Routes>{routes}</Routes>;
};

中遇到的错误,

,最后是我从initialRoute

stacktrace

“

I'm trying to pass a route from one component to another, and I'm getting this error.

TypeError: Cannot read properties of undefined (reading 'pathname')

I don't know why I'm getting this error.

this is my useRouting

const useRouting = () => {
    const { primaryRole } = useAuth();
    const { asapUser } = useAsapContext();
    const [initialRoute, setInitialRoute] = useState(null);

    const routesMetadataForRole = useMemo(() => getRoutesForRole(primaryRole), [primaryRole]);

    const routes = useMemo(() => routesPerRole(routesMetadataForRole), [routesMetadataForRole]);

    useEffect(() => {
        if (primaryRole === 'asap-dept-member') {
            apiService.ApplicationService.getMemberApplication(asapUser.id).then(response => {
                const application = response.find(application => application.applicant.user.id === asapUser?.id);
                if (application !== null) {
                    setInitialRoute(routesMetadataForRole[2]?.path.replace(':id', application?.id));
                } else {
                    setInitialRoute(routesMetadataForRole[0]?.path);
                }
            });
        } else {
            setInitialRoute(routesMetadataForRole[0]?.path);
        }
    }, [primaryRole, routesMetadataForRole, asapUser.id]);

    return { routes, routesMetadataForRole, initialRoute };
};

and this is my BaseLayoutRoutes

const BaseLayoutRoutes = () => {
    const navigate = useNavigate();
    const { routes, initialRoute } = useRouting();
    const location = useLocation();

    useEffect(() => {
        if (location.pathname === '/' && routes && routes.length > 0) {
            navigate(initialRoute, { replace: true });
        }
    }, [initialRoute, navigate, location.pathname, routes]);

    return <Routes>{routes}</Routes>;
};

and finally an error I'm getting from initialRoute

enter image description here

stacktrace

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜`诱少女 2025-02-04 06:05:40

似乎错误和StackTrace通知您,initialRoute值传递给navigate> navigate函数是未定义的。

仅当存在initialRoute值时,仅发出重定向。

const BaseLayoutRoutes = () => {
  const navigate = useNavigate();
  const { routes, initialRoute } = useRouting();
  const location = useLocation();

  useEffect(() => {
    if (location.pathname === '/' && routes?.length && initialRoute) {
      navigate(initialRoute, { replace: true });
    }
  }, [initialRoute, navigate, location.pathname, routes]);

  return <Routes>{routes}</Routes>;
};

It seems the error and stacktrace is informing you that the initialRoute value that is passed to the navigate function is undefined.

Only issue the redirect if the initialRoute value exists.

const BaseLayoutRoutes = () => {
  const navigate = useNavigate();
  const { routes, initialRoute } = useRouting();
  const location = useLocation();

  useEffect(() => {
    if (location.pathname === '/' && routes?.length && initialRoute) {
      navigate(initialRoute, { replace: true });
    }
  }, [initialRoute, navigate, location.pathname, routes]);

  return <Routes>{routes}</Routes>;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文