【问题标题】:Can't catch exception in AngularFireAuth无法在 AngularFireAuth 中捕获异常
【发布时间】:2021-08-07 08:00:14
【问题描述】:

我尝试使用 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", ...}
    }
  }
}

依赖关系:

  • @angular: 10.2.4
  • @angular/fire: 6.1.4
  • 火力基地:8.2.2

【问题讨论】:

  • 你能解决这个问题吗?你最后做了什么?我遇到了同样的问题。

标签: angular firebase firebase-authentication angularfire


【解决方案1】:

您可以通过管道捕获错误来代替 try/catch 块:

await this.af.createUserWithEmailAndPassword(email,password)
    .catch(err => {
        console.log(error); 
    })

【讨论】:

  • 这不起作用。 catch 回调将运行,但错误也会冒泡到全局错误处理程序,只是问题发生在 try/catch
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 2021-10-14
  • 2019-11-27
  • 2014-05-02
  • 2017-03-15
  • 2013-11-23
  • 2017-09-16
相关资源
最近更新 更多