Amaury Leproux
Amaury Leproux

Reputation: 229

React Native, Navigation: "Undefined is not an object"

I'm trying to redirect my user when his login succeed but I got this error: "Undefined is not an object (evaluating '_this2.props.navigation')"

This is my code in App.js:

const Homepage = StackNavigator({
 Home: {screen: Home},
 Store: {screen: Store},
 Admin: {screen: Admin},
});

This is my code in Store.js where I try to redirect the user once he logged in:

<Button style={{color:'gray', borderColor: 'gray'}} onPress={() => 
this.login()} title="Me connecter" />

login(){
    firebase.auth().signInWithEmailAndPassword(this.state.emailLogin, 
    this.state.passwordLogin).then(function(user) {
        // Send the user to the next step
        setTimeout(() => {
            const { navigate } = this.props.navigation; 
            navigate('Admin');
        }, 1500);
    }).catch(function(error) {
        console.log('error');
        var errorCode = error.code;
        var errorMessage = error.message;

        if (errorCode === 'auth/wrong-password') {
            alert('Wrong password.');
        } else {
            alert(errorMessage);         
        }
        console.log(error);
    });
}

Thanks in advance for your time and your precious answers.

Upvotes: 0

Views: 235

Answers (1)

bennygenel
bennygenel

Reputation: 24680

To be able use this you need to bind the function,

Change this code,

firebase.auth().signInWithEmailAndPassword(this.state.emailLogin, 
    this.state.passwordLogin).then(function(user) { /* rest of your code */ })

to this,

firebase.auth().signInWithEmailAndPassword(this.state.emailLogin, 
    this.state.passwordLogin).then((user) => { /* rest of your code */ })

or this,

firebase.auth().signInWithEmailAndPassword(this.state.emailLogin, 
    this.state.passwordLogin).then(function(user) { /* rest of your code */ }.bind(this))

Upvotes: 1

Related Questions