密码输入错误如何停止导航?



密码输入错误时如何停止导航。我的应用程序在捕获错误后仍然重定向。我试过这个简单的If条件,但它似乎不起作用。

login = (email,password)=>{
try{
firebase.auth().signInWithEmailAndPassword(email,password)
.then(function(user){
console.log(user)
}); 
}
catch(error){
console.log(error.toString())
if(error===true) { 
return; //stop the execution of function
}
}
this.props.navigation.replace('Home')
}

简单地说,这样做…我认为,没有必要再写try catch

login = (email,password)=>{
const nav = this.props.navigation;

firebase.auth().signInWithEmailAndPassword(email,password)
.then( user => {
console.log(user)
nav.replace('Home')
}). catch( error => {
console.log(error) 
}) 


}

你的版本是正确的,但error不是布尔值,所以你不能检查它是否为=== true,但你可以通过简单地检查if (error)是否存在,它应该工作

login = (email,password)=>{
try {
firebase.auth().signInWithEmailAndPassword(email,password)
.then(function(user){
console.log(user)
}); 
} catch(error) {
console.log(error.toString())
if(error) { 
return; //stop the execution of function
}
}
this.props.navigation.replace('Home')
}

最新更新