简体   繁体   English

在我单击UI之前,signInWithPopup Promise不会执行.catch。 Angular和Firebase

[英]signInWithPopup promise doesn't execute the .catch until I click the UI. Angular and Firebase

I'm having a trouble with the .signInWithPopup() method provided by AngularFireAuth, you can see more here: firebaseAuthReference 我在AngularFireAuth提供的.signInWithPopup()方法上遇到麻烦,您可以在此处查看更多信息: firebaseAuthReference

In my auth.service.ts I've the following method. 在我的auth.service.ts中,我使用以下方法。

 signinWithFacebook2() {
        const provider = new firebase.auth.FacebookAuthProvider();
        return this.afAuth.auth.signInWithPopup(provider);
      }

afAuth is been injected in the auth's constructor: 将afAuth注入auth的构造函数中:

constructor(private router: Router,
              private afAuth: AngularFireAuth){}

I call signinWithFacebook2() when the user click a button in my login.component.ts (click event). 当用户单击我的login.component.ts中的按钮(单击事件)时,我调用signinWithFacebook2()。

onFacebookLogin() {
    this.authService.signinWithFacebook2()
      .then(
        (res) => {
          this.authService.getTokenId();
          this.router.navigate(['/dashboard']);
        }
      )
      .catch(
        (err) => {
          this.showError = true;
          // TODO fix bug. this code isn't execute until I press the button again.
        }
      );
  }

When the promise is resolved everything is OK, the code is executed correctly but when the reject is been accomplished the code isn't executed until I press the login button again. 兑现诺言后,一切正常,代码将正确执行,但拒绝完成后,直到再次按下登录按钮,代码才执行。 This is a strange behavior.Hope you have understanded my problem, if not please leve a comment. 这是一种奇怪的行为。希望您了解我的问题,否则请发表评论。

try to force change detection on error. 尝试强制对错误进行更改检测

  1. add import: 添加导入:

     import { ChangeDetectorRef } from '@angular/core'; 
  2. pass reference to constructor: 将引用传递给构造函数:

     constructor(private router: Router, private ref: ChangeDetectorRef, private afAuth: AngularFireAuth) {} 
  3. force change detection on error: 强制更改错误检测:

     (err) => { this.showError = true; // TODO fix bug. this code isn't execute until I press the button again. this.ref.detectChanges(); ) 

From the docs you provided it looks like you have to catch the errors as the second argument of the .then() function. 从您提供的文档中,您似乎必须将错误作为.then()函数的第二个参数来捕获。

onFacebookLogin() {
    this.authService.signinWithFacebook2()
      .then(
        (res) => {
          this.authService.getTokenId();
          this.router.navigate(['/dashboard']);
        },
      (err) => {
          this.showError = true;
          // TODO fix bug. this code isn't execute until I press the button again.
        }
      );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM