2017-09-17 75 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

相關問題