如何根据日期对Firebase用户进行排序?

时间:2019-11-21 18:37:19

标签: javascript firebase sorting

我正在尝试向非技术人员代表Firebase用户。他想把最新的用户放在首位。我在名为joinDate的每个Firebase用户节点上都有这个键值对。我想基于此提取数据。

这是代码

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++;
                }
            });
        });

我尝试做

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

这不起作用,或者我的数据看起来像这样:- Firebase image

1 个答案:

答案 0 :(得分:0)

您的joinDate属性以dd/MM/yyyy格式保存字符串值。由于它们是字符串值,因此按字典顺序对其进行排序。并且按字典顺序,例如"01/15/2020""21/11/2019"之前。

要允许按时间顺序排序,您有两个主要选项:

  1. 将日期作为实际Date值存储在Firestore中。
  2. 将日期存储在字符串中,以确保其字典顺序与时间顺序相同,例如"2019-11-21""2020-01-15"之前。

另请参阅: