Jakub Balicki
Jakub Balicki

Reputation: 55

Getting user data from firebase (ionic)

So I have created function which sets username, firstname and lastname of currently logged user, it looks like this:

  profile = {} as Profile;
  
  ...
  
  createProfile() {
    this.afAuth.authState.take(1).subscribe(auth => {
      this.afDatabase.object(`profile/${auth.uid}`).set(this.profile)
        .then(() => this.navCtrl.setRoot(MainPage));
    })
  }

the data on firebase looks like this:

enter image description here

Now, I would like to create new function in a new page called user.ts:

showdata() { }

So it would display username, firstname, and lastname of the user on user.html

the question is how to do this?

Upvotes: 0

Views: 1813

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80952

To retrieve the data, use the below:

 firebase.database().ref().child("profile").on('value', function(snapshot) {
snapshot.forEach(function(child) {
var datas = child.val();
var firstname=child.val().firstname;
var lastname=child.val().lastname;
var username-child.val().username;
  });
});

If you have the userid then you can do child("profile").child(userid).on(..){..} and retrieve without using forEach()

https://firebase.google.com/docs/database/web/read-and-write

Upvotes: 1

Related Questions