[Meteor/React] How to re-sort data on the client?


#1

Hi people of Meteor, I hit a wall trying to solve this.

I have publish method that sorts the collection by some criteria and also limits the amount of entries it returns.
On the client I have a load more button.
What happens is: first N entries that I get are sorted, but as soon as I click on the load more button, minimongo isn’t sorted anymore.

I read online that I’m supposed to “re-sort” the data once it hits the client, but after spending a lot of time searching for solution online I came empty handed. The fact that I’m using React is also making it a bit harder for me, as I’m kinda learning React at the same time.

Code
Server:

  Meteor.publish('all_views', function(per_page) {
    return Entries.find({}, {
      sort: {views: -1},
      limit:per_page
    });
  });

Client:

export default createContainer(({type, sort}) => {
      console.log("createContainer should sub to: " + type + "_" + sort);

      // set up subscriptions
      Meteor.subscribe(type + "_" + sort, PER_PAGE);

      return {entries: Entries.find({}).fetch()};
    }, EntrieslList);

I hope someone can help me. I can provide more code if needed, I’d just like to arrive at solution.
Thanks!


Meteor React filter for sorting results
#2

You can also pass a sort specifier to the find() method on the client, for example:

      return {entries: Entries.find({}, {sort: {views: -1}}).fetch()};
    }, EntrieslList);

This will cause a sort to take place on the documents stored in the client side collection (i.e within Minimongo).

Hope this helps!


#3

That solved it! Thanks Longmate!