Can I send function as subscribe parameter

This isn’t working. I get “object is not a function”.

PlayersList = new Mongo.Collection('players');  

if (Meteor.isServer) {

  Meteor.publish('PlayersPublished', function(paramFunc, filterIndex){
    var filter = paramFunc(filterIndex);
    return PlayersList.find(filter);


if (Meteor.isClient) {
  function paramFunc(index) {  
    if (index==0) { return {                score : 10 }; }
    if (index==1) { return {name : "David", score : 10 }; }

  Meteor.subscribe('PlayersPublished', paramFunc, 0);


Is these ideal?

In lib folder:
functionparam1 = function() {
  return PlayersList.find({score: 10});
functionparam2 = function() {
  return PlayersList.find({name : "David", score : 10 });

In server folder:
Meteor.publish('collection', function(index) {
   if(index==1){return functionparam1();}
   if(index==2){return functionparam2();}

In client folder:

Since Functions are not JSON serializable, i guess you cannot send a function to the server.
Anyway, that would be a bad idea on a security point of view, if the server has some code to execute, it should come from the server, imho.

1 Like

Don’t try to pass a function, because you can’t. Put the logic from the function in the publication and just pass the index as a parameter. You can have multiple publications if you need and the client can subscribe to wher whichever it needs.

1 Like