Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order by child value in Firebase

I have data like this:

    scores: {
      uid1: {
        score: 15,
        displayName: "Uciska"
      },
      uid2: {
        score: 3,
        displayName: "Bob"
      },
      uid3: {
        etc...
      }
    }

I want to rank them by score and keep only the top 100.

I did that by following the doc. But it does not work. It always returns the same order even if the score changes.

    const query = firebase.database().ref('scores')
      .orderByChild('score')
      .limitToLast(100)

      query.on('child_added', snapshot => {
        const score = snapshot.val().score
        console.log(score)
    })

I added that too in the rules to optimize but I'm not sure it's correct:

    "scores": {
      ".indexOn": ["score"]
    }

What is the right way to go?

like image 878
uciska Avatar asked Dec 24 '22 05:12

uciska


1 Answers

Your code is correct and should show the desired result.

You may encounter difficulties to see the result due to the 'child_added' event since "the listener is passed a snapshot containing the new child's data", as detailed here in the doc.

You could use the once() method, as follows, which will show the result a bit more clearly since it will display the entire set of scores.

    const query = firebase.database().ref('scores')
        .orderByChild('score')
        .limitToLast(100)

    query.once('value', function (snapshot) {
        snapshot.forEach(function (childSnapshot) {
            var childKey = childSnapshot.key;
            var childData = childSnapshot.val();
            console.log(childData);
            // ...
        });
    });

Also, your rule can be written as ".indexOn": "score" since there is only one parameter.

like image 192
Renaud Tarnec Avatar answered Jan 22 '23 05:01

Renaud Tarnec