Skip to content Skip to sidebar Skip to footer

How To Get The Position Of An Item In The Realtime Database

I want to get the position of an item in the database, along with the data. .ref('users') .orderByChild('email') .equalTo(myEmail) .once('value', snapshot => { console.log('

Solution 1:

The Firebase Realtime Database does not track the position of nodes in their parents, nor does it return such a position in query results. This means the only way to determine the position of a child within a set of child nodes, is to retrieve all child nodes before the one you're looking for.

.ref("users")
.orderByChild("email")
.endAt(myEmail)
.once("value", snapshot => {
    var index = 0;
    snapshot.forEach(function(childNode) {
        if (childNode.child("email").val() === myEmail) {
            console.log("Database Position", index);
        }
        else {
            index = index + 1;
        }
    });
}

Since you commented that you're trying to create a leaderboard, see:


Post a Comment for "How To Get The Position Of An Item In The Realtime Database"