【发布时间】:2022-02-01 23:19:05
【问题描述】:
所以我试图从引用中提取所有数据,但使用 orderByChild 函数似乎没有正确排序,这是我的代码:
function show_users_posts() {
document.getElementById('post-spinner').style.display= "none"
var timestampfinal
const database = firebase.database()
database.ref('FLSocial/Accounts/' + myParam + '/Posts')
.orderByChild('sortDate')
.limitToLast(10) // get 10 most recent entries
.once('value', function (snapshot) { // <-- consider Promise API instead
const sortedChildren = [];
snapshot.forEach(childSnapshot => { sortedChildren.unshift(childSnapshot) }); // reverses the order
sortedChildren.forEach(childSnapshot => {
const childKey = childSnapshot.key;
const childData = childSnapshot.val();
/* rest of the code */
var description = childData.Description
var image = childData.Thumbnail
var link = childData.Link
var seconds = childData.Seconds
var created = parseInt(seconds)
// The time now
var now = new Date().getTime();
// The difference between now and created
var howLongAgo = created - now;
// Convert to a positive integer
var time = Math.abs(howLongAgo);
// Define humanTime and units
var humanTime, units;
// If there are years
if (time > (1000 * 60 * 60 * 24 * 365)) {
humanTime = parseInt(time / (1000 * 60 * 60 * 24 * 365), 10);
units = 'years';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// If there are months
else if (time > (1000 * 60 * 60 * 24 * 30)) {
humanTime = parseInt(time / (1000 * 60 * 60 * 24 * 30), 10);
units = 'months';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// If there are weeks
else if (time > (1000 * 60 * 60 * 24 * 7)) {
humanTime = parseInt(time / (1000 * 60 * 60 * 24 * 7), 10);
units = 'weeks';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// If there are days
else if (time > (1000 * 60 * 60 * 24)) {
humanTime = parseInt(time / (1000 * 60 * 60 * 24), 10);
units = 'days';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// If there are hours
else if (time > (1000 * 60 * 60)) {
humanTime = parseInt(time / (1000 * 60 * 60), 10);
units = 'hours';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// If there are minutes
else if (time > (1000 * 60)) {
humanTime = parseInt(time / (1000 * 60), 10);
units = 'minutes';
timestampfinal = humanTime + ' ' + units + ' ago'
}
// Otherwise, use seconds
else {
humanTime = parseInt(time / (1000), 10);
units = 'seconds';
timestampfinal = humanTime + ' ' + units + ' ago'
}
console.log(timestampfinal)
//childKey
//childData.FirstName
var html = `<div class="col">`;
html += `<div class="card h-100">`;
if (image == "") {
html += `<img height="300px" width="150px" src="img/No-Image-Placeholder.svg.png" class="card-img-top" alt="...">`;
}
else {
html += `<img src="${image}" class="card-img-top" alt="...">`;
}
html += `<div class="card-body">`;
if (verificationstatusforuser == "Verified") {
html += `<h4 class="card-title">${myParam}<i class="material-icons" style="color: #458eff">verified</i></h5>`;
}
else if (verificationstatusforuser == "Owner") {
html += `<h4 class="card-title">${myParam}<i class="material-icons" style="color: #458eff">verified_user</i></h5>`;
}
else if (verificationstatusforuser == "Domain") {
html += `<h4 class="card-title">${myParam}<i class="material-icons" style="color: #458eff">domain</i></h5>`;
}
else if (verificationstatusforuser = "False") {
html += `<h4 class="card-title">${myParam}</h5>`;
}
html += `<p class="card-text">${description}</p>`;
if (link == "") {
}
else {
html += `<a href="${link}" target="_blank" type="button" class="btn btn-dark">Open Attached Link</a>`;
}
html += `</div>`;
html += `<div class="card-footer">`;
html += `<small class="text-muted">${timestampfinal}</small>`;
html += `</div>`;
html += `</div>`;
html += `</div>`;
document.getElementById('post-section').innerHTML += html;
})
});
}
它抓取并记录所有父母,但它不是由“sortDate”孩子订购的。通常是:-1643676999661,据说 Idk 为什么它不起作用,但它不会抛出错误,它只是不想对其进行排序
这是来自 firebase 控制台的 JSON 数据(已导出):
{
"FLSocial" : {
"Accounts" : {
"BigECheese" : {
"AccountInformation" : {
"Owner" : "tMcdvvK25FYtRbtHiTKw4RzUI542"
},
"AccountSettings" : {
"Bio" : "Welcome To The Beta Of FL Social"
},
"Posts" : {
"1-31-2022-1643676999661" : {
"Description" : "Welcome To The Beta Of FL Social, You are able to post updates to your wall, Add Links To Them, Add Photos Too Them",
"Link" : "https://google.com",
"Seconds" : 1643676999661,
"Thumbnail" : "",
"TimeStamp" : "1-31-2022-1643676999661",
"sortDate" : -1643676999661
}
},
"ProfilePicture" : {
"DataURL" : "../../icons/etech-logo"
},
"Verification" : {
"Status" : "False"
}
}
}
},
"Permissions" : {
"Admins" : {
"Emails" : "test123@fl-cloud.com"
},
"Owners" : {
"Emails" : "admin@fl-cloud.com"
}
}
}
【问题讨论】:
-
与上一个问题相同,我们需要查看显示问题的代码。请编辑您的问题以显示:1)您查询的与问题相关的 JSON(作为文本,请不要截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。 2)实际显示问题的代码,例如使用
console.log就像在您之前的问题中一样,3)您得到的输出以及您的预期。 -
我刚刚更新并提供了我能提供的所有信息,没有问题通过控制台记录,所以我无法记录问题
-
我怀疑重现问题所需要的所有代码。请仔细研究how to create a minimal, complete, verifiable example,因为按照那里的指导,我们更有可能提供帮助。 --- 而不是生成所有的 HTML,而是输出不是你期望的值——当我问的时候你做了here。仅通过该日志记录并向我们显示其输出,它应该能够最小程度地重现问题。
标签: javascript firebase-realtime-database