0

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"
    }
  }
}
Ethan Marr
  • 49
  • 7
  • Same as in your previous question, we'll need to see the code that shows the problem. Please edit your question to show: 1) the JSON that you query as far as it is relevant to the problem (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). 2) the code that actually shows the problem, e.g. with a `console.log` just like in your previous question, 3) the output you get and what you expected instead. – Frank van Puffelen Feb 01 '22 at 02:31
  • I Just Updated And Gave Every Bit Of Information I Could, Theres no issue That gets logged via console so i cant log the issue – Ethan Marr Feb 01 '22 at 02:57
  • I doubt all that code is needed to reproduce the problem. Please study [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) carefully, as following the guidance in there makes it much more likely we can help. --- Instead of generating all the HTML, output the value that isn't what you expect it to be - just you did [here](https://stackoverflow.com/q/70923798) when I asked. With just that logging and by showing us the output of that, it should be able to minimally reproduce the problem. – Frank van Puffelen Feb 01 '22 at 04:30

1 Answers1

0

So I figured out a complex but simple math solution, I made a global variable, starting at 100

Then I made it so that way in the for each function it counts up one in the variable, and when I go to submit a post I set the key of the post’s data to the global variable, this way it started sorting in recent down to oldest, I appreciate your help frank for helping me diagnose ways to fix this

Ethan Marr
  • 49
  • 7