I have a publish on the server:
if (Meteor.isServer) {
Meteor.publish("search", function (query) {
var re = new RegExp(query, "i");
var chartsFound = Chart.find({"demographics.lastName": {$regex: re}}, {limit: 500, sort: {"demographics.lastName": 1}});
return chartsFound;
});
and then a template helper on the client:
Template.searchResults.helpers({
theCharts: function () {
var re = new RegExp(Session.get('searchString'), "i");
var chartsFound = Chart.find({"demographics.lastName": {$regex: re}}, {limit: 500, sort: {"demographics.lastName": 1}});
return chartsFound;
}
});
And a search form that takes the search term and sets the session variable and subscribes to that search term:
Template.navbar.events = {
'submit #searchForm': function (e) {
e.preventDefault();
Router.go('/search');
var searchString = e.target.liveSearch.value;
Session.setPersistent('searchString', searchString);
searchSubsManager.clear();
searchSubsManager.subscribe("search", searchString);
}
}
and this works well … I change the session variable “searchString” and the search refreshes. What I would like to do now is what I believe I read somewhere as a suggested practice, and that is to move the “Find” logic into a meteor.method, because as you can see, I have the same logic coded twice:
Chart.find({"demographics.lastName": {$regex: re}}, {limit: 500, sort: {"demographics.lastName": 1}});
so, here is the method I tried to define:
Meteor.methods({
findChartsByLastName: function (lastName) {
var re = new RegExp(lastName, "i");
var chartsFound = Chart.find({"demographics.lastName": {$regex: re}}, {limit: 500, sort: {"demographics.lastName": 1}});
return chartsFound;
}
});
and then I tried to call it from the publish:
Meteor.publish("search", function (query) {
var chartsFound = Meteor.call('findChartsByLastName', query);
return chartsFound;
});
But, when I did this I got the error “Exception from sub search id…”
Any help would be greatly appreciated! thank you.