[英]React native navigation error -TypeError: Cannot read property 'catch' of undefined
I rerouting user when proper data are loaded from Async Storage:当从异步存储加载正确的数据时,我重新路由用户:
useEffect(() => {
loadData(localStorageKeys.USER_LOGGED_IN, true)
.then(isUserLoggedIn => {
if (isUserLoggedIn) {
props.navigation.navigate(NavigationLocations.DASHBOARD)
.catch(e => logNavigationError(e))
} else {
console.log("No user is signed in.");
console.log(AsyncStorage.getAllKeys());
}
}
)
.catch(e => {
console.error("Unable check locally if user is logged in.");
debugger
console.error(e);
});
}, []);
The routing works as intended, however I am getting this error:路由按预期工作,但是我收到此错误:
index.js:1 Unable check locally if user is logged in.
console.<computed> @ index.js:1
(anonymous) @ Signup.js:32
Promise.catch (async)
(anonymous) @ Signup.js:29
...
Why is the error occuring even though the routing happens as intended?即使路由按预期发生,为什么还会发生错误?
I think this is because navigation.navigate
is not a Promise, and so it doesn't have the property .catch
.我认为这是因为navigation.navigate
不是 Promise,所以它没有属性.catch
。 If you want to catch an error here, instead use try/catch
.如果您想在此处捕获错误,请改用try/catch
。
eg例如
useEffect(() => {
loadData(localStorageKeys.USER_LOGGED_IN, true)
.then(isUserLoggedIn => {
if (isUserLoggedIn) {
try {
props.navigation.navigate(NavigationLocations.DASHBOARD);
} catch (e) {
logNavigationError(e);
}
} else {
console.log("No user is signed in.");
console.log(AsyncStorage.getAllKeys());
}
}
)
.catch(e => {
console.error("Unable check locally if user is logged in.");
debugger
console.error(e);
});
}, []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.