Here Is My Main Code:
function show_users_posts() {
document.getElementById('post-spinner').style.display= "none"
var timestampfinal
const database = firebase.database()
database.ref('Cloud/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;
})
});
}
//And The Code Continues, But This is the main part i wanna fix
Here is my Firebase Data Tree:
"Cloud": {
"Accounts": {
"User1": {
"Posts": {
"1-31-2022-1643620733159": {
"sortDate": 1643620733159,
}
"1-31-2022-1643620742152": {
"sortDate": 1643620742152,
}
}
}
}
}
Now All I Want To Do is reverse the order making the most recent posts on top and the oldest on bottom, but the code im using doesnt even change the order, and i dont know why, iv tried order by Key, Iv tried everything, nothing is working, The Reference Is Correct Because It Loads All My Posts But Not In The Right Order
Edited Code - adding the log to console:
var database = firebase.database()
database.ref('Cloud/Accounts/' + myParam + '/Posts').orderByChild('sortDate').once('value', function (snapshot) {
snapshot.forEach(function(childSnapshot) {
var childKey = childSnapshot.key;
var childData = childSnapshot.val();
console.log(childData.sortDate);
Logs This:
1643620733159
1643620742152
It Logs Each Value, But I Need It The Other Way Around.