我尝试使用 AngularFireAuth.createUserWithEmailAndPassword 捕获异常并向用户显示存在的用户名/密码错误消息。异常处理代码运行但不知何故类似的异常仍然冒泡到全局错误处理程序。
import { Component } from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
@Component({
selector: 'register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent {
constructor(
private af: AngularFireAuth,
) {}
async onRegisterUsingEmailAndPassword(event) {
const { email, password } = event;
try {
await this.af.createUserWithEmailAndPassword(email, password);
} catch(error) {
console.log(error); // <-- this code runs and logs {code: "auth/email-already-in-use", ...}
}
}
}
使用 firebase.auth 直接按预期工作:异常代码处理程序运行并且没有异常冒泡到全局错误处理程序。
import { Component } from '@angular/core';
import firebase from 'firebase/app';
@Component({
selector: 'register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.scss']
})
export class RegisterComponent {
constructor() {}
async onRegisterUsingEmailAndPassword(event) {
const { email, password } = event;
try {
await firebase.auth().createUserWithEmailAndPassword(email, password);
} catch(error) {
console.log(error); // <-- this code runs and logs {code: "auth/email-already-in-use", ...}
}
}
}
依赖:
答案 0 :(得分:0)
您可以通过管道捕获错误以代替 try/catch 块:
await this.af.createUserWithEmailAndPassword(email,password)
.catch(err => {
console.log(error);
})