Search code examples
meteormeteor-blazeflow-router

How to access FlowRouter subscriptions in Meteor template helpers?


it seems like I can't access a FlowRouter template subscription in my helper. How can you do this?

In my server code:

Meteor.publish('AllUsers', function() {
    return Meteor.users.find({}, {fields: {profile: 1}});
})

In my router code:

var userRoutes = FlowRouter.group({
    subscriptions: function(params, queryParams) {
        this.register('AllUsers', Meteor.subscribe('AllUsers'));
    },
});

In my template code:

{{#if checkFlowRouterSubs}}
    {{#each getTheUsers}}
        {{>userPartial}}
    {{/each}}
{{/if}}

In my helpers I have the 'guard':

checkFlowRouterSubs: function() {
    if (FlowRouter.subsReady()) {
        return true;
    };
    return false;
},

And then the getTheUsers helper:

...
var users = AllUsers.find(filterObject, { sort: { 'profile.firstname': 1 } }).fetch(); // the actual query definitely works
...

But I get an error:

Exception in template helper: ReferenceError: AllUsers is not defined

I should note that in the getTheUsers helper, FlowRouter.subsReady('AllUsers') returns true


Solution

  • so, first, this :

    var userRoutes = FlowRouter.group({
        subscriptions: function(params, queryParams) {
            this.register('AllUsers', Meteor.subscribe('AllUsers'));
        },
    });
    

    is NOT server code: it is Client code: the Flow-router is a client side router: counter intuitive but this is the basis of all these routers. The hint here is that you are 'subscribing' to the publication in this code, so it is on the client side.

    Iron-Router is routing both on the server and client-side so it makes things even more confusing when you come from there.

    What you are missing here is the publish function on the server side.

    Meteor.publish('AllUsers', function() {
        return AllUsers.find();
    });
    

    EDIT:

    The Error

    Exception in template helper: ReferenceError: AllUsers is not defined seems like because you did not define the collection on the client side

    var AllUsers = Mongo.Collection('AllUsers'); //or whatever the actual collection