mato
mato

Reputation: 151

Iterate over child values in firebase database

This is my firebase database:

database

I am trying to make function with one parameter, which will then find bars, which have the beer, that matches the parameter value; and I have no idea how to iterate over beers in every bar. The firebase documentation is not really clear for me.

Code:

var bars = firebase.database().ref("bars").orderByChild("rating");

function barsByBeer(key){
    bars.once("value").then(function(snap){
        snap.forEach(function(childSnapshot){
            *something should go here, but I am not sure what*
        });
    });

Upvotes: 0

Views: 1254

Answers (1)

Cappittall
Cappittall

Reputation: 3441

I think you need to find out the bars which only have some keys equal true (assuming these keys are indicating a beer brand :)

function barsByBeer(key){
   var selectedBar =[];
   bars.once("value", snap=> {
        snap.forEach(b=>{
            if (b.val().beers[key]) { // Assuming you will provide a beer key
               selectedBar.push(b.val().name);
            }
        });
    });
}

At this above code, you will have an array list called selectedBar of bars which have the beers (described with keys). Hope helps. If it does not work, try to console.log b item into forEach loop to make an adjustment.

Upvotes: 2

Related Questions