1

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);
})


}
Stuart Fong
  • 603
  • 1
  • 13
  • 21
  • Possible duplicate of [Sort and limit not working with Mongo + Meteor](https://stackoverflow.com/questions/45957640/sort-and-limit-not-working-with-mongo-meteor) – Styx Sep 05 '17 at 04:02
  • When you insert your note, i suggest you to set the field createdAt with `new Date()` instead of `Date.parse(new Date())`. It gives you more flexibility cause it store the date object, and you can still do the sort. – Gaëtan Rouziès Sep 05 '17 at 05:29

2 Answers2

1

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

this.tracker = Tracker.autorun(() => {
  const notes = Notes.find({},{sort: {createdAt: -1}).fetch()
Mikkel
  • 7,693
  • 3
  • 17
  • 31
0

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

Sachin
  • 3
  • 1