How to pass an object to a publish method

Is it possible to pass an object to a Meteor.publish() method?

I have a bunch of checkboxes to filter search results. With each change in the check box I loop through and add all the checked values to an array and then to a session variable. I’m trying to get that into the mongo query and failing miserably at it.

In my Template.onCreated:

    var filters = Session.get('applyFilters') || null;
    var handle = self.subscribe('frontAllProductsFilter', filters);

In my checkbox event map:

    filters = [];
    var out = {};
    // get checked checkboxes values
    $("input:checked").each(function () {
        var id = $(this).attr("id").toString();
        filters.push( id );
    out["color"] = filters;
    Session.set('applyFilters', out);

And finally my publish method (please fogive all the if statements, I’m trying anything at this point!!!

Meteor.publish('frontAllProductsFilter', function(filter){
        if(filter.color !== undefined && filter.type !== undefined){
            console.log("both filters");
            // need to build this query after I can get a single filter working!
        if(filter.color && filter.color !== "" && filter.type === undefined){
            console.log("only color: "+ filter['color']);
            return Products.find({active: true, color: { $in: filter.color }});
        if(filter.type && filter.color === undefined){
            console.log("only type");
            return Products.find({active: true, type: { $in: filter.type }});
        return Products.find({active: true});

When I output the filter.color or filter.type it comes back as a string with no quotes…if I have multiple filters selected they are comma separated but with no quotes so I don’t know if it’s being converted as a string or I’m just writing it like that or what is happening. At the end of the day I’m trying to filter my subscription between type and color. The user can select multiples of each so that’s why I’m trying the $in operator if I’m wrong please, please tell me the right way I’m creating quite the dent in my desk by the amount of times I’ve banged my head into it :smile:

The short answer is yes. I have done this successfully myself.

So, the long answer is that (given the short answer) there must be something wrong elsewhere in your code. Have you ensured the object is being generated/set/retrieved correctly on the client and is appearing with the same structure and values at the top of your publish code?

To simplify and enhance your publish logic, have you considered using check?

The object is set in the event map. Really I’m just using a temporary variable ( out ) which is emptied everytime a check box is changed and then that variable is set into the Session. I hadn’t thought of using check thanks for the suggestion. When I send the publish argument to the console at the top of the code, I’ll get a properly formatted object (using stringify) if I try to out put the property I want ( filter.color ) I simply get the value with no quotes and not in an array.