So im trying to pull all the data from a reference, but using the orderByChild function it doesnt seem to order it properly, Here is my code:
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;
})
});
}
It Grabs And Logs All The Parents But Its Not Ordering It By The "sortDate" Child. Which is usually: -1643676999661, With That Being Said Idk Why Its Not Working, But It Doesnt Throw Back Errors, It Just Doesnt Wanna Sort It
Here is the JSON data from the firebase console(exported):
{
"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"
}
}
}