Meteor - sorting by timestamp

寵の児 提交于 2019-12-24 11:44:14

问题


I have a note taking app which allows users to add notes. I am trying to render newly created notes on the home page, but when I go to the add note and add a note, then go back to the home page, it shows the new notes(limited to 10) but the new note that was just created is at the bottom of the list, instead of the top. When I refresh the page, it goes to the top like I want it to. How would I get it so that I don't have to refresh the page to get the newly created note at the top?

I try to sort by using a timestamp, (using Date.parse(new Date())), and then publishing from server using this,

Meteor.publish('notes-newest', function () {
    return Notes.find({},{sort: {createdAt: -1}, limit: 10});
});

Here is where I am trying to render it:

componentDidMount() {
Meteor.subscribe('notes-newest')
this.tracker = Tracker.autorun(() => {
  const notes = Notes.find().fetch()
  if(notes == null || notes == undefined){
    return;
  }
  this.setState({ notes });
  console.log(notes);
})


}

回答1:


You need to do the sort on the client as well.

this.tracker = Tracker.autorun(() => {
  const notes = Notes.find({},{sort: {createdAt: -1}).fetch()



回答2:


If still you dont get the data as you want then you can try below code

    this.tracker = Tracker.autorun(() => {
    const notes = Notes.find({},{sort: {createdAt: -1}).fetch()
    var data = notes.reverse()

You will get your data in descending order while displaying



来源:https://stackoverflow.com/questions/46045393/meteor-sorting-by-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!