2017-09-17 51 views
0

我试图重定向我的用户,当他的登录成功,但我得到这个错误:“未定义不是一个对象(评估'_this2.props.navigation')”React Native,导航:“未定义不是一个对象”

这是我在App.js代码:

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

这是我在Store.js代码,我尝试将用户重定向一旦他登录:

<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); 
    }); 
} 

在此先感谢为您的时间和宝贵的答案。

回答

1

为了能够使用this需要绑定功能,

改变此密码,

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

此,

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

或此,

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

这两个函数都必须绑定这个或箭头函数。我的意思是setTimeout的函数也应该绑定。 – Kyon

+0

@Kyon'setTimeout'已经是一个箭头功能,如果我看得很对 – bennygenel

+0

好吧,你是对的! – Kyon

相关问题