Faire
Faire

Reputation: 1011

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?

Upvotes: 0

Views: 92

Answers (1)

fippi
fippi

Reputation: 534

I think this is because navigation.navigate is not a Promise, and so it doesn't have the property .catch. If you want to catch an error here, instead use try/catch.

e.g.

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);
            });
    }, []);

Upvotes: 1

Related Questions