Meteor publication and subscription not working

50 views Asked by At

I am working on an admin page that shows a list of the user's orders. For some reason, publication function is not receiving the user's ID as a parameter from my subscription function.

My user object contains the attributes:

_id
userId
confirmed

My orders object contains the attributes:

_id
userId
amount
date

My publication:

Meteor.publish('clientOrders', function(userId) {
    console.log('user is ' + userId);
    return Order.find({_id: userId});
});

My subscription:

viewClientOrders = RouteController.extend({
    loadingTemplate: 'loading',
    waitOn: function () { 
    Meteor.subscribe('clientOrders', this.params._id);
},
action: function() {
    this.render('viewClientOrders');
}

});

My route:

Router.route('/viewClientOrders/:id', {name: 'viewClientOrders', controller: 'viewClientOrders', onBeforeAction: requireLogin});

I did a console.log for the userId and it returns null, although I clearly passed a userId parameter (this.params._id) in my subscription function. However, if I pass a parameter of 3, for testing purposes, it will work. Also, I used a similar publication/subscription method to view a client's profile, passing in the exact same parameters and it works fine...

Anyone know what's going on? Thanks!

1

There are 1 answers

4
Tarang On

Make sure you return in your waitOn. Iron Router will only proceed to render if it knows the subscription is ready:

viewClientOrders = RouteController.extend({
    waitOn: function () { 
        return Meteor.subscribe('clientOrders', this.params._id);
    }
    ...

Make sure you've also set a loadingTemplate (docs).