使用本机反应将错误显示为关于 firebase 身份验证的警报
Show error as alert on firebase authentication with react native
我在我的 React 本机应用程序中使用此功能,但它使我的应用程序崩溃,尽管它在我的模拟器上显示错误作为控制台,我该如何使用警报向用户显示错误而不是崩溃应用程序?
signup =(email,password) =>{
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home')) }
catch(error){
alert("not valid"`enter code here`)
}
}
为什么不
signup =(email,password) =>{
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home'))
.catch(error => {
alert("Error occurred", error)
})
}catch(err){
alert("Error occurred", err);
}
}
您可以在错误代码上使用 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);
}
如果您想直接显示来自 firebase 的消息:
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home'))
.catch(error => {
alert(error.message);
})
}catch(err){
alert(err);
}
我认为你不能在 try 块中使用异步函数或 promise 我之前已经这样做过并且它导致了与你应该使用的相同的问题然后捕获并尝试删除 try 语句我希望对你有帮助
我在我的 React 本机应用程序中使用此功能,但它使我的应用程序崩溃,尽管它在我的模拟器上显示错误作为控制台,我该如何使用警报向用户显示错误而不是崩溃应用程序?
signup =(email,password) =>{
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home')) }
catch(error){
alert("not valid"`enter code here`)
}
}
为什么不
signup =(email,password) =>{
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home'))
.catch(error => {
alert("Error occurred", error)
})
}catch(err){
alert("Error occurred", err);
}
}
您可以在错误代码上使用 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);
}
如果您想直接显示来自 firebase 的消息:
try{
firebase.auth().createUserWithEmailAndPassword(email,password)
.then(() => this.props.navigation.navigate('home'))
.catch(error => {
alert(error.message);
})
}catch(err){
alert(err);
}
我认为你不能在 try 块中使用异步函数或 promise 我之前已经这样做过并且它导致了与你应该使用的相同的问题然后捕获并尝试删除 try 语句我希望对你有帮助