ElJefeJames ElJefeJames - 3 months ago 15
React JSX Question

How do I specify which Meteor Subscription should be used to render in React?

I have multiple Subscriptions and Publications for different views on the same collection. One of these views is used for returning a Text Search on the Mongo Collection, whilst other views return the entire collection.

My problem is when I try to view the results on client, I'm not sure how to specify to use the search result subscription. Currently my client view is displaying the entire collection instead of the restricted search results. How do I specify which subscription should be used? Or am I misunderstanding the pub/sub model? Possibly I should only be using a single subscription? Any input appreciated!

// Server side publication
Meteor.publish("search", function(searchValue){
if (!searchValue) {
console.log("there is no search value");
return remoteData.find({});
}

console.log("there is a search value and it is " + searchValue);
return remoteData.find({$text:{$search: searchValue}});
});

Meteor.publish("allData", function (){
return remoteData.find();
});

// Client Side subscription
var searchSubscription = Meteor.subscribe("search", searchQuery);
var allDataSubscription = Meteor.subscribe("allData");

// inside React Component
// this returns everything, so I think it's using the allDataSubscription
filteredData() {
return (
remoteData.find({}).fetch();
)
}

Answer

In your case you don't really need to have two separate subscriptions. Just merge things into one subscription, so there is never any doubt. For example:

// Server
Meteor.publish('myData', function (searchValue) {
  const selector = {};
  if (searchValue) {
    selector.$text = {
      $search: searchValue,
    };
  }
  return remoteData.find(selector);
});

// Client
const myDataHandle =  Meteor.subscribe('myData', searchQuery);
...
remoteData.find({}).fetch();