Lafoune
Lafoune

Reputation: 476

Firebase: How to read of children from different child in javascript?

i know the question up there is confusing but here what i want to read from firebase in explained manner:

enter image description here

As it obvious from the image, i want retreive all circled three child's "route_name" and conosle log it in javascript. To do that i try like this:

var buses_Ref = firebase.database().ref('online_drivers/');
buses_Ref.on('child_added', function (data) {
     console.log(data.val().route_name);
});

But it showing undefined

enter image description here

And if I tried to console log data it will showing this, which i think are the two children Jigjiga and Siinaay

console.log(data);

enter image description here

Upvotes: 0

Views: 64

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You have two nested dynamic child nodes under online_drivers. By using child_added Firebase takes care of one of those already, but you'll need to handle the other level in your callback code.

The easiest way to do this is with DataSnapshot.forEach():

buses_Ref.on('child_added', function (snapshot) {
  snapshot.forEach(function(data) {
    console.log(data.val().route_name);
  })
});

Upvotes: 1

Related Questions