Nick
Nick

Reputation: 25

What am I doing wrong in displaying data? - Ionic 3 & Firebase

I'm having trouble displaying some Firebase data in my Ionic 3 app. Here's the relevant code from my component (the abcdef part is just a substitute for a particular user's key):

var ref = firebase.database().ref('/profiles/abcdef/');
this.viewProfile = ref.once("value")
  .then(function(snapshot) {
    var firstName = snapshot.child("firstName").val();
    var lastName = snapshot.child("lastName").val();
    var orgName = snapshot.child("orgName").val();
    console.log(firstName+' '+lastName+' works at '+orgName );
  });

And here's what I have in the view:

{{viewProfile.firstName}}

No error, simply nothing displays. Any ideas?

Upvotes: 1

Views: 83

Answers (2)

Nick
Nick

Reputation: 25

You were very close @Hareesh:

var ref = firebase.database().ref('/profiles/abcdef/');
    ref.on('value' , profileSnapshot => {
              this.viewProfile = profileSnapshot.val();
     });

and

{{viewProfile.firstName}}

(no async needed) seems to work.

Thank you!!!

Upvotes: 1

Hareesh
Hareesh

Reputation: 6900

You need to loop the snapshot to get the data

try this

var ref = firebase.database().ref('/profiles/abcdef/');
ref.once("value")
  .then(function(snapshot) {
      snapshot.forEach(function(data) {
          this.viewProfile = data.val();
      }
 });

Upvotes: 0

Related Questions