angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

.update method of FirebaseObjectObservable doesn't fire

Open profesori opened this issue 9 years ago • 2 comments

Hi,

I have a problem with the update() method of an FirebaseObjectObservable.

I've declared a service with these methods :

@Injectable()
export class AuthProvider {

  public firebaseUser: Observable<firebase.User>
  public currentUser: firebase.User
  constructor(public afAuth: AngularFireAuth, public db: AngularFireDatabase) {
    this.firebaseUser = this.afAuth.authState}

  isAuthentificated() {
    return this.firebaseUser
  }

  loginUser(newEmail: string, newPassword: string) {
    return Observable.create(observer => {
      this.afAuth.auth.signInWithEmailAndPassword(newEmail, newPassword).then((data) => {
        observer.next(true)
      }).catch((error) => {
        observer.error(error);
      });
    })
  }

  signupUser(newEmail: string, newPassword: string, username: string) {
    return Observable.create(observer => {
      this.afAuth.auth.createUserWithEmailAndPassword(newEmail, newPassword).then((data) => {
        const user = new User
        user.nom = username
        user.email = newEmail
        this.setFirebaseUserData(user)
        this.updateDisplayName(username)
        observer.next(true)
      }).catch((error) => {
        observer.error(error);
      });
    })
  }

  logoutUser() {
    firebase.database().goOffline();
    //this.currentUser.next(null);
    return this.afAuth.auth.signOut();
  }
  getCurrentUserData() {
    console.log("Calling : getFirebaseUserData : ", this.afAuth.auth.currentUser)
    return this.db.object('/USER_INFO/' + this.afAuth.auth.currentUser.uid)
  }

  setFirebaseUserData(user: User) {
    console.log("Create new user : ", user, this.afAuth.auth.currentUser.uid)
    this.db.object('/USER_INFO/' + this.afAuth.auth.currentUser.uid).update(user)
  }

  updateDisplayName(name: string) {
    this.afAuth.auth.currentUser.updateProfile({
      'displayName': name, 'photoURL': ""
    })
  }

}

when i call signupUser() from a component, the user in created sucssesfully but setFirebaseUserData() doesn't fire and i can't uderstand why. it seems that the update() method isn't working.

profesori avatar Aug 23 '17 12:08 profesori

Have u got the solution.

raviphilips avatar Aug 23 '17 13:08 raviphilips

Yes @raviphilips , i wait for the observable setFirebaseUserData response and then call observer.next(true) like this :

  signupUser(newEmail: string, newPassword: string, username: string) {
    return Observable.create(observer => {
      this.afAuth.auth.createUserWithEmailAndPassword(newEmail, newPassword).then((data) => {
        const user = new User
        user.nom = username
        user.email = newEmail
        this.setFirebaseUserData(user).then(() => {
          this.updateDisplayName(username)
          observer.next(true)
        })
      }).catch((error) => {
        observer.error(error);
      });
    })
  }

profesori avatar Aug 23 '17 13:08 profesori