简体   繁体   English

反应本机导航错误-TypeError:无法读取未定义的属性'catch'

[英]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.

相关问题 反应本机TypeError:无法读取未定义的属性“导航” - react native TypeError: Cannot read property 'navigation' of undefined React.js-TypeError:无法读取未定义的属性“ catch” - React.js - TypeError: Cannot read property 'catch' of undefined React Native无法读取属性未定义错误 - React Native cannot read property undefined error 类型错误:无法读取 React Native 中未定义的属性“set” - TypeError: Cannot read property 'set' of undefined in React native "React-Native AsyncStorage:TypeError:无法读取未定义的属性“setItem”" - React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined React Native [TypeError:无法读取未定义的属性&#39;startsWith&#39;]? - React Native [TypeError: Cannot read property 'startsWith' of undefined]? React Native TypeError:无法读取未定义的属性'projectName' - React Native TypeError: Cannot read property 'projectName' of undefined react-native - 类型错误:无法读取未定义的属性“修剪” - react-native - TypeError: Cannot read property 'trim' of undefined 类型错误:无法读取未定义的属性“导航”-反应本机 - TypeError: Cannot read property 'navigate' of undefined - React native 未捕获的类型错误:无法读取未定义的属性“映射”(REACT NATIVE - Javascript) - Uncaught TypeError: Cannot read property 'map' of undefined ( REACT NATIVE - Javascript )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM