0

Firebase question: I'm trying to retrieve data back from a firebase database in order from greatest to smallest and then display it in the html. However, I haven't had any success. I know how to get the data back but I can't get it in order from greatest to smallest. Is this possible or do I have to sort the data when it comes back?

Here is the code I'm using.

``for (var x in allUsers) {

                //db.ref('users/' + x).orderByChild('score').startAt(10000).on('value', snapshot => {
                db.ref('users/' + x).orderByChild('score').endAt(9999).on('value', snapshot => {
                //db.ref('users/' + x).on('value', snapshot => {
                    console.log( snapshot.val());

                    const newUserRow = $("<tr>");
                    const userName =  $("<th> scope ='row'> ").html(snapshot.val().firstName);
                    const userLevel = $("<td>").html(snapshot.val().level);
                    const userPoints = $("<td>").html(snapshot.val().score);

                    newUserRow.append(userName)
                            .append(userLevel)
                            .append(userPoints);
                        $("#LeaderboardInformation").append(newUserRow);
                });``

My thought process for this line of code is: 1) Retrieve from database 'users' 2) Get the 'score' key 3) Start where the score is '10000'.

1 Answers1

0

You can duplicate the score field as "negative_score" for example, multiplying it by -1 on every update. It's like shadowing the field. With Firebase Realtime Database limitations, the mindset is all about denormalizing everything you can when you write to the DB so you can achieve what you need.

This way you can sort it the way you want to.

hecht
  • 292
  • 2
  • 6