【问题标题】:How to Stop Navigation If user enters wrong password?如果用户输入错误的密码,如何停止导航?
【发布时间】:2021-02-28 13:46:46
【问题描述】:

如果用户输入错误的密码,如何停止导航。发现错误后,我的应用程序仍在重定向。我已经尝试过这个简单的 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')
}

【问题讨论】:

  • 为什么要在 Promise 回调之外导航?我认为导航应该依赖auth响应,所以应该放在then块中

标签: javascript reactjs react-native react-navigation


【解决方案1】:

简单,就这样吧。。我觉得,不用多写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) 
       }) 
    
        
}

【讨论】:

  • 您好,这是正确的答案吗?我试过这样做,但它仍然引导我到下一页
【解决方案2】:

您的版本是正确的,但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')
}

【讨论】:

  • 仍在导航到下一个屏幕,然后我收到错误消息(错误:密码无效或用户没有密码。)
猜你喜欢
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
相关资源
最近更新 更多