React Native,导航:“未定义不是对象”
2017-09-17
230
我试图在用户登录成功时重定向用户,但出现此错误:“未定义不是对象(评估‘_this2.props.navigation’)”
这是我在 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))
bennygenel
2017-09-17