使用 react native 将错误显示为 firebase 身份验证的警报

Sḧî*_*ṗṫā 0 native reactjs

我在我的本机应用程序中使用此功能,但它使我的应用程序崩溃,尽管它在我的模拟器上显示错误作为控制台,我应该如何使用警报向用户显示任何错误而不是使应用程序崩溃?

  signup =(email,password) =>{
try{
    firebase.auth().createUserWithEmailAndPassword(email,password)

    .then(() => this.props.navigation.navigate('home')) } 
    catch(error){
        alert("not valid"`enter code here`)
    }

   }
Run Code Online (Sandbox Code Playgroud)

Pra*_*een 8

您可以在错误代码上使用 switch case 显示您的自定义警报消息:

import {Alert} from 'react-native';

   try{
    firebase.auth().createUserWithEmailAndPassword(email,password)
      .then(() => this.props.navigation.navigate('home'))
      .catch(error => {   
        switch(error.code) {
          case 'auth/email-already-in-use':
                Alert.alert('Email already in use !')
                break;
       }
     })
   }catch(err){
      alert("Error : ", err);
   }
Run Code Online (Sandbox Code Playgroud)

如果您想直接从 firebase 显示消息:

   try{
    firebase.auth().createUserWithEmailAndPassword(email,password)
      .then(() => this.props.navigation.navigate('home'))
      .catch(error => {   
        alert(error.message);
     })
   }catch(err){
      alert(err);
   }
Run Code Online (Sandbox Code Playgroud)