Firebase Database reading data

I get a snapshot from Firebase database as follows:

firebase.database().ref('/explore/').once('value', (snapshot) => {

});

snapshot is an object with several levels:

user ID1 (“123123123”)

  • name
  • age
  • ID
    User ID2 (“234235235”)
  • name
  • age
  • ID
    And so no…

I need to display in a list all of the "name"s of all of the users. And I don’t have the ID’s of all of the users (except in the Database). I can’t get my head around to using a for loop to loop through all the available users (ID’s). Any help will be greatly appreciated!

1 Like

You should be able to do something like this to get everything in an array.

firebase.database().ref('/explore/').once('value', (snapshot) => {
  let users = [];
  snapshot.forEach( snap => {
    users.push(snap.val()); //or snap.val().name if you just want the name and not the whole object
  });
});
1 Like

Thanks! I’m still getting this error:

And 10 hours later, here is the solution:

let users = [];
   snapshot.forEach(snap => {
     users.push(snap.val()); //or snap.val().name if you just want the name and not the whole object
     return false;   //<=THIS IS WHAT WAS MISSING!!!!!
   });
1 Like

Hi, i had the same issue, but i want to return the value of snap.val().name instead return false

There’s an error of snaphot when i try to do this.

Do you now another way to obtain the value calling a method of another class? (i try to do MVC model)

I 've tried with a “Promise.resolve”, with “.then” but the value came undefined or like an object.

Thanks for any help!