0

I am trying to represent firebase users to a non technical person. He wants the newest users on top. i have this key value pair on every firebase user node called joinDate. i want to pull data out based on that.

Here is the code

ref.on('value', function(snapshot) {
            var i = 1;
            snapshot.forEach(function(childSnapshot) {
            var childData = childSnapshot.val();
            console.log(childData);
            if (childSnapshot) {
                var emailText = childSnapshot.child("userProfile/email").val();
                var joinDateText = childSnapshot.child("joinDate").val();
                var currentPhyxText = childSnapshot.child("userProfile/currentPhyx").val();
                var nameText = childSnapshot.child("userProfile/name").val();
                var lastNameText = childSnapshot.child("userProfile/lastname").val();
                var progressText = childSnapshot.child("userProfile/progress");
                var lastSeenDateText = childSnapshot.child("userProfile/score/lastDate").val();
                var StringifiedProgressText = JSON.stringify(progressText);

                var email = document.createElement("h4");
                var number = document.createElement("h4");
                var joinDate = document.createElement("h6");
                var currentPhyx = document.createElement("h6");
                var name = document.createElement("h6");
                var lastName = document.createElement("h6");
                var lastDate = document.createElement("h6");
                var lowbackData = document.createElement("h6");
                var progress = document.createElement("h6");
                var br = document.createElement("br");

                email.style.display = "inline";               
                number.style.display = "inline";
                lastName.style.display = "inline";
                name.style.display  = "inline";

                joinDate.textContent = "Join Date :  " + joinDateText;        
                currentPhyx.textContent = "Current Phyx : " + currentPhyxText;
                number.textContent = i+") ";
                email.textContent = emailText;
                name.textContent = "Name : " + nameText;
                lastName.textContent = " " + lastNameText;
                lastDate.textContent = "Last Seen Date : " + lastSeenDateText;
                progress.textContent = "Progress : " + StringifiedProgressText;
                progress.textContent = progress.textContent.replace(/{/g, "");
                progress.textContent = progress.textContent.replace(/}/g, "");
                progress.textContent = progress.textContent.replace(/"/g, "");
                progress.textContent = progress.textContent.replace(/,/g, " • ")

                document.body.appendChild(number);
                document.body.appendChild(email);
                document.body.appendChild(joinDate);
                document.body.appendChild(currentPhyx);
                document.body.appendChild(name);
                document.body.appendChild(lastName);
                document.body.appendChild(lastDate);
                document.body.appendChild(progress);
                document.body.appendChild(br); 
                i++;
                }
            });
        });

I tried doing

ref.orderByChild("joinDate").on('value', function(snapshot) {
...
...
}

This doesn't work either my data looks like this :- Firebase image

Phyxable
  • 109
  • 10
  • 2
    What exactly does "this doesn't work" mean? Given that `joinDate` is a String, my guess is the result is ordered by month first or something? You should use a timestamp instead and format it for display. –  Nov 21 '19 at 18:46

1 Answers1

1

Your joinDate properties hold string values in dd/MM/yyyy format. Since they are string value, they are ordered lexicographically. And in lexicographical order, for example, "01/15/2020" comes before "21/11/2019".

To allow chronological ordering you have two main options:

  1. Store the dates as actual Date values in Firestore.
  2. Store the dates in strings that ensure the lexicographical order it the same as the chronological order, e.g. "2019-11-21" comes before "2020-01-15".

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hey thank you for the answer i tried doing, `var newdate = date.split("/").reverse().join("-"); console.log("New Date is ", newdate);`. but it returns the date as `yyyy-dd-mm`. Can you please help me convert it to `yyyy-mm-dd`. – Phyxable Nov 22 '19 at 18:02
  • `var parts = "01/15/2020".split("/"); result = parts[2]+"-"+parts[0]+"-"+parts[1];` – Frank van Puffelen Nov 22 '19 at 18:09