作者:佛山越野e族99_990 | 来源:互联网 | 2023-08-12 10:03
我正在尝试向非技术人员代表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
您的joinDate
属性以dd/MM/yyyy
格式保存字符串值。由于它们是字符串值,因此按字典顺序对其进行排序。并且按字典顺序,例如"01/15/2020"
在"21/11/2019"
之前。
要允许按时间顺序排序,您有两个主要选项:
- 将日期作为实际
Date
值存储在Firestore中。
- 将日期存储在字符串中,以确保其字典顺序与时间顺序相同,例如
"2019-11-21"
在"2020-01-15"
之前。
另请参阅:
- Firebase query by date string(适用于实时数据库,但问题和解决方案完全相同)
- How to retrieve data from firebase where the value of a date is 1 month ago from current date
- How does Firestore query operation strings treat different data types?
- How to order data from Firestore in Flutter,orderBy not ordering correct