How to retrieve multiple keys in Firebase? [duplicate]

Considering this data structure:

{
    "users":{
        "user-1":{
            "groups":{
                "group-key-1":true,
                "group-key-3":true
            }
        }
    },
    "groups":{
        "group-key-1":{
            "name":"My group 1"
        },
        "group-key-2":{
            "name":"My group 2"
        },
        "group-key-3":{
            "name":"My group 3"
        },
        "group-key-4":{
            "name":"My group 4"
        },
    }
}

I could get the groups of a particular user with:

firebase.database().ref('users/user-1/groups').on(...)

Which would give me this object:

{
    "group-key-1":true,
    "group-key-3":true
}

So now I want to retrieve the info for those groups.

My initial instinct would be to loop those keys and do this:

var data = snapshot.val()
var keys = Object.keys(data)

for (var i = 0; i < keys.length; i++) {
    firebase.database().ref('groups/' + keys[i]).on(...)
}

Is this the appropriate way to call multiple keys on the same endpoint in Firebase?

Does Firebase provide a better way to solve this problem?

Is this the appropriate way to call multiple keys on the same endpoint
in Firebase?

Yes, generally this is a good solution to retrieve every group this way.

Does Firebase provide a better way to solve this problem?

I don’t think Firebase provides any other function/query that could help in this case.

Anyway, this could be improved saving the ref in a variable and looping on the keys of the object directly. Also, if you just need to retrieve those data once, you should use once() instead of on()

var groupRef =  firebase.database().ref('groups/')
var data = snapshot.val()

for (var groupID in data) {
    groupRef.child(data[groupID]).once(...)
}

Another way could be using Firebase’s functions for the data snapshots, like forEach

snapshot.forEach(function(childSnapshot) {
      groupRef.child(childSnapshot.key).once(...)
})

Yes, you are going in the right way. As written in this question firebase will pipeline your requests and you won’t have to be concerned about performance and roundtrip time. As soon as your iteration starts you will be receiving firebase data. So keep in mind to handle it properly in your ui.

Read More:   Using fadein and append

Another option, that will depends on how big your /groups data will grow, is to keep a snapshot (could be a $firebaseObject if you are using angularfire) of the entire /groups branch that will refresh whenever data changes. Then you just have to track this snapshot. But again, if you are planning to play with a big amount of groups your current solution is a better choice.

Also, I recommend you to take care of setting an on event for each group you retrieve. Keep in mind that the callback will be triggered whenever the data changes (depends on the setted event). Depending on your use case you should consider using .once("value" since it will trigger the data only once, making it more reliable, more performatic and will avoid handling callbacks when you are not expecting them.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts