2

I have multiple subscriptions on the search results page of a Meteor app. So in the middle is a template for the search results of items, on the left is a template for trending items, and on the right is a template for related items.

On the server I publish a related items (on the right) by querying the Mongodb using a text search which is only possible on the server since minimongo doesn't have that functionality.

But I am also subscribing to a trending items (on the left) which grabs a different set of those same items. In isolation, I receive the correct results, that is, when I comment out the code for the trending items, I get the right results for the related items. And vice versa. But when both are left, it appears they are drawing from the same collection on the client and the results are distorted.

Is there any way to handle multiple subscriptions on the same page?

trendingItems.js

Meteor.subscribe('trendingItems');

Template.trendingItems.helpers ({

  trendingItems: function() {
    results = Items.find({}, {
      fields : { follows :  1, title: 1, itemId: 1 },  
      sort   : { follows : -1 }, 
      limit  : 5
    }).fetch();
    return results;
  }
});

relatedItems.js

Template.relatedItems.helpers ({

  relatedItems: function() {
    return Items.find();
  }

});

publications.js

Meteor.publish('relatedItems', function(searchString) {
  return Items.find(
    { $text: { $search: searchString } }
  );
});

Meteor.publish('trendingItems', function(options) {

  results = Items.find({}, {
    fields : { follows :  1, title: 1, itemId: 1 },
    sort   : { follows : -1 },
    limit  : 5
  }).fetch();
  return results;

});

A general solution to the problem of handling multiple subscriptions, rather than a specific solution that solves just this problem, is desirable.

anon
  • 2,143
  • 3
  • 25
  • 37
  • all these subscriptions are going into the same collection; this answer should help you understand all that: http://stackoverflow.com/a/18880927/1087119 – Christian Fritz Aug 29 '15 at 22:24
  • your best bet is probably to follow the counts-by-room example from http://docs.meteor.com/#/full/meteor_publish, i.e., create a new collection for the text-search results and publish into that, based on a cursor of the `Items` collection. – Christian Fritz Aug 29 '15 at 22:44
  • Also check iron-router which will also help you out. – Luna Sep 10 '15 at 00:41

0 Answers0