TypeError:无法读取未定义的属性(Reading' PathName')错误
我正在尝试将路线从一个组件传递到另一个组件,并且遇到了这个错误。
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>;
};
中遇到的错误,
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
stacktrace
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎错误和StackTrace通知您,
initialRoute
值传递给navigate> navigate
函数是未定义的。仅当存在
initialRoute
值时,仅发出重定向。It seems the error and stacktrace is informing you that the
initialRoute
value that is passed to thenavigate
function is undefined.Only issue the redirect if the
initialRoute
value exists.