2

我正在学习如何编码。我正在为承诺以及如何使用它们而苦苦挣扎。

我想使用 Facebook 和 Firebase 完成登录。

当我不将其用作服务时,该代码可以完美运行

  authWithFacebook(){
    this.usersRef.authWithOAuthPopup("facebook", (error) => {
      if (error) {
        console.log(error);
      }else if (this.isLoggedIn && this.newUser) {
        this.usersRef.child(this.authData.uid).set({
          NomComplet: this.authData.facebook.displayName,
          ProfileCached: this.authData.facebook.cachedUserProfile,
          Nom : this.authData.facebook.cachedUserProfile.last_name,
          Prenom : this.authData.facebook.cachedUserProfile.first_name,
          ProfileImg: this.authData.facebook.profileImageURL,
          Agemoyen : this.authData.facebook.cachedUserProfile.age_range,
          Localite : this.authData.facebook.cachedUserProfile.locale,
        });
      }
    });
    console.log("je suis connecté" + " " + this.authData.facebook.displayName )
  }

我尝试将我的代码转换为可以在整个应用程序中使用的服务。但它不起作用:

authWithOAuth(){
  return new Promise(function(resolve, reject){
    this.usersRef.authWithOAuthPopup("facebook", (error) => {
      if (error) {
        console.log(error);
        reject(error);
      }else {
        resolve();
      }
    })
  })
}

谁能帮助我或告诉我要阅读哪个文档以完全理解这一点?

4

1 回答 1

1

您需要像这样重构您的代码:

authWithFacebook(){
  this.authService.authWithOAuth().then(
    () => {
      this.usersRef.child(this.authData.uid).set({
        NomComplet: this.authData.facebook.displayName,
        ProfileCached: this.authData.facebook.cachedUserProfile,
        Nom : this.authData.facebook.cachedUserProfile.last_name,
        Prenom : this.authData.facebook.cachedUserProfile.first_name,
        ProfileImg: this.authData.facebook.profileImageURL,
        Agemoyen : this.authData.facebook.cachedUserProfile.age_range,
        Localite : this.authData.facebook.cachedUserProfile.locale,
      });
    },
    (err) => {
      console.log(error);
    }
  );
}

使用thenpromise的方法。

于 2016-02-13T13:41:25.440 回答