Display data in order by date IONIC/FIREBASE

#1

Hello,

I have one more thing to finish my application …

I use ionic with Firebase and I post comments on a page! However, I can not display them in order by date. They appear in the total order (two hours,one minute,three hours…).

Thanks …

#2

You can try like this

firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("all", data.map(function(val) { return new Date(val.timestamp).toString(); }));
});

Good Luck
Thanks

1 Like
#3

Also you can try like this

posts.component.ts

export class PostsComponent {
  posts: FirebaseListObservable<any>;

  constructor(db: AngularFireDatabase) {
    this.posts = db.list('/posts', {
      query: {
        orderByChild: 'date'
      }
    });
  }
}

posts.component.html

<div *ngFor="let post of (posts | async)?.slice().reverse()">
  <h1>{{ post.title }}</h1>
</div>
2 Likes
#4

It’s working fine !! Thanks you … !! :slight_smile:

#5

if its work fine please mark the solution.

#6

What’s this “endpoint”?

#7

You enlightened me!
It really helped me a lot.