2016-02-13 12 views
2

Я учу код. Я борюсь с обещанием и как их использовать.Как использовать Promise в angular2 с firebase для преобразования входа в систему

Я хочу выполнить вход с помощью 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(); 
     } 
    }) 
    }) 
} 

Может ли кто-нибудь помочь мне с этим или сообщить мне, какой документ читать, чтобы это полностью понять?

ответ

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); 
    } 
); 
} 

использовать then метод обещаний.

 Смежные вопросы

  • Нет связанных вопросов^_^