小编Tua*_*Ngo的帖子

无法捕获 AngularFireAuth 中的异常

我尝试使用 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", ...}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

直接使用 firebase.auth 按预期工作:异常代码处理程序运行并且没有异常冒泡到全局错误处理程序。

import { Component } from '@angular/core';
import firebase from 'firebase/app';

@Component({
  selector: 'register',
  templateUrl: …
Run Code Online (Sandbox Code Playgroud)

firebase angularfire firebase-authentication angular

7
推荐指数
1
解决办法
521
查看次数