Ad

Ionic Firebase Get User Information From Real Time Database

I am trying to get user information (for example: username) in my Firebase Database but in .ts file, I could not figure out how to code it

I tired using auth.uid to get the username from Database but did not work. the below is the code that I have used to save user profile. it works well.

this.afAuth.authState.pipe(take(1)).subscribe(auth => {
  let path= 'userProfile'+'/'+ auth.uid;
  this.afDatabase.object(path).set(this.profile);
})

But I cannot figure out how to get its info. So in my database, under "userProfile" there is "username". How do I use this logic to get the username? I need to use that auth.uid to display the username in my app

Ad

Answer

It seems that you have the following database:

userProfile
      userid
        username : peter

You can do the following:

export class DataComponent implements OnInit{
  ref : any;
  constructor(db: AngularFireDatabase, public afAuth: AngularFireAuth) {
     ref = db.list('/userProfile', ref => ref.orderByKey().equalTo(this.afAuth.auth.currentUser.uid)).valueChanges();
    }

  ngOnInit(){
    ref.subscribe(items => {
       console.log(items);  
    });
  }

}

Here you query on the node userId and then using that node you can retrieve the username.

Check the following:

https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

Ad
source: stackoverflow.com
Ad