【问题标题】:Angular nested promises doesn't complete角嵌套承诺没有完成
【发布时间】:2020-10-25 21:33:23
【问题描述】:

我正在为我的应用程序中的连续/嵌套承诺而苦苦挣扎。它用于通过 Firebase 进行身份验证,但我相信(根据我所阅读的内容,尽管仍然是初学者),问题在于我如何嵌套承诺并可能返回它们。

我尝试了不同的教程,这些教程似乎相同,但不知何故对我不起作用。我找不到与 tutos 有什么不同。我也试过Promise.all,但这也没有用。嵌套的承诺 persistTempUserData 永远不会解析,因此不会写入数据库。在一次绝望的尝试中,我开始玩弄 return 或者只是调用该方法而不返回任何内容,但这仍然不起作用。我还查看了与在线类似的其他问题,但找不到与我相似的问题。

登录组件

  public tryGoogleLogin(): void {
    console.log("tryGoogleLogin")
    this.authService.googleLogin().then(() => {
      console.log("Gooogle login worked")
    }).catch(() => {
      console.log("Google login didn't workd")
    })
  }

认证服务

  public googleLogin(): Promise<void> {
    return this.oAuthLogin(new auth.GoogleAuthProvider());
  }

  private oAuthLogin(provider: auth.GoogleAuthProvider | auth.FacebookAuthProvider): Promise<void> {
    return new Promise((resolve) => { // Also tried not returning a promise and called this... directly. Didn't work
      this.angularFireAuth.auth.signInWithPopup(provider)
        .then((credential: auth.UserCredential) => {
          if (credential.additionalUserInfo.isNewUser) { // Checked with Console log and is true
            this.persistTempUserData(credential.user).then((res: any) => {
              resolve(); // Never Resolved
            })
          }
        }).catch((error: any) => {
          console.log("signInWithPopup failed", error);
        });
    })
  }

  private persistTempUserData(user: firebase.User): Promise<void>  {
    console.log("persistTempUserData called") // Called
    const userRef: AngularFirestoreDocument<any> = this.angularFirestore.collection(Constants.fbPathRefUsersNode).doc(user.uid)

    const userData: User = {
      uid: user.uid,
      profilePic: null,
      firstName: this.nameSplitterFirstName(user.displayName),
      lastName: this.nameSplitterLastName(user.displayName),
      email: user.email,
      dOB: null,
      phoneNum: null,
      gender: null,
      institution: null,
      isAccValid: false
    }

    return userRef.set(userData, {
      merge: true
    });
  }

【问题讨论】:

    标签: angular firebase firebase-authentication es6-promise


    【解决方案1】:

    试试这个:

    我纠正了 2 个问题:

    1. persistTempUserData 没有返回承诺。

    2. 如果以下条件评估为假,则没有返回语句。

        if (credential.additionalUserInfo.isNewUser) { 
          return this.persistTempUserData(credential.user);
        }
    
        
    public googleLogin(): Promise<void> {
      return this.oAuthLogin(new auth.GoogleAuthProvider());
    }
    
    private oAuthLogin(provider: auth.GoogleAuthProvider | auth.FacebookAuthProvider): Promise<void> {
      return new Promise((resolve, reject) => { // Also tried not returning a promise and called this... directly. Didn't work
        this.angularFireAuth.auth.signInWithPopup(provider)
          .then((credential: auth.UserCredential) => {
            if (credential.additionalUserInfo.isNewUser) { // Checked with Console log and is true
              return this.persistTempUserData(credential.user);
            }
            return reject(new Error("Some Message"));
          }).then((res: any) => {
            return resolve();
          }).catch((error: any) => {
            console.log("signInWithPopup failed", error);
            return reject(error);
          });
      })
    }
    
    private persistTempUserData(user: firebase.User): Promise<void>  {
        return new Promise((resolve) => {
        console.log("persistTempUserData called") // Called
        const userRef: AngularFirestoreDocument<any> = this.angularFirestore.collection(Constants.fbPathRefUsersNode).doc(user.uid)
    
        const userData: User = {
          uid: user.uid,
          profilePic: null,
          firstName: this.nameSplitterFirstName(user.displayName),
          lastName: this.nameSplitterLastName(user.displayName),
          email: user.email,
          dOB: null,
          phoneNum: null,
          gender: null,
          institution: null,
          isAccValid: false
        }
        return resolve(userRef.set(userData, {
          merge: true
        }));
        })
    }
    
    

    【讨论】:

    • 嗨。谢谢你的时间。可惜还是没写。然而,您仍然会在 Firebase 的 Anthentication 页面中创建帐户,在日志中写入“persistTempUserData called”,但在数据库中没有写入任何内容。也完全没有错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多