barrylachapelle
barrylachapelle

Reputation: 987

Firebase - traversing children without knowing the parent?

enter image description here

Hi all,

Im just wondering if it's possible to loop through children without knowing the parent node in Firebase. My structure right now is BOOKS/USERID/BOOKID I would love to loop through ALL the books without knowing the USERID or the parent. I'm hoping to do this from a Cloud Function. Is this possible?

Upvotes: 0

Views: 35

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599176

If you want to loop over all books of all users in Node.js:

var rootRef = admin.database().ref("books");
return rootRef.once("value", function(booksSnapshot) {
  booksSnapshot.forEach(function(userSnapshot) {
    userSnapshot.forEach(function(bookSnapshot) {
      console.log(bookSnapshot.key, bookSnapshot.val());
    });
  });
  return true; // to flag that we're done
});

Upvotes: 1

Related Questions