Alan Simpson
Alan Simpson

Reputation: 13

Firebase JavaScript snapshot get values

When I grab a snapshot of one item from a Firebase realtime database as below, when I console.log the snapshot.val and just the key, I can see all the individual properties and their values in console.log. But I can't see to get at them. For example, there's a lastvisited property in there, but snapshot.val().lastvisited just returns undefined. I can't figure out the syntax for getting that value of that one property?

 pageCountersRef.orderByChild('location').equalTo(newcount.location).once('value', function (snapshot) {
            if (snapshot.val()) {
                console.log(snapshot.val());
                var arr = Object.keys(snapshot.val());
                var key = arr[0];
                console.log(key);

}

Here's what I see in console.log. Seems I should be able to grab .lastreffer, .lastvisit, .location, and .vists out of there, but everything I try returns undefined, even though I can see the values in there in the console.

enter image description here

Upvotes: 0

Views: 243

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Try the following:

pageCountersRef.orderByChild('location').equalTo(newcount.location).once('value', function (snapshot) {
snapshot.forEach(function(child) {
            var lastvisit=child.val().lastvisit;
            var keys=child.key;
          });
       });

You need to use forEach to be able to iterate inside the properties and get their values.

Upvotes: 0

Related Questions