Meteor: Publishing all user not working without autopublish package

250 views Asked by At

I would to show a list of all users, in my template. I have:

//publications.js
Meteor.publish('users', function() {
    return Meteor.users.find({}, { fields: {username: 1, profile: 1} });
});

//router.js
Router.route('/users/add/:_id?', {name: 'users.add', controller: 'UserAddController'});

UserAddController = RouteController.extend({
  subscriptions: function(){
    return [ Meteor.subscribe('hospitals'), 
            Meteor.subscribe('roles'),
            Meteor.subscribe('users') ];
  },
  action: function() {
    this.render('addUser', { 
      data: function(){
        return { hospital_id : this.params._id }
      }
    });
  }
});


//client
Template.listUsers.helpers({
  users: function() {
    return Meteor.users.find({});
  }
});

But the list keep showing only the current logged-in user. I have created a list of users using Account.createUser() function What am I doing wrong?

Thanks.

2

There are 2 answers

0
Tomas Hromnik On

You have to subscribe to a publication using this.subscribe() in subscriptions hook:

// a place to put your subscriptions
subscriptions: function() {
  this.subscribe('items');

  // add the subscription to the waitlist
  this.subscribe('item', this.params._id).wait();
}

Or use waitOn:

// Subscriptions or other things we want to "wait" on. This also
// automatically uses the loading hook. That's the only difference between
// this option and the subscriptions option above.
waitOn: function () {
  return Meteor.subscribe('post', this.params._id);
}
0
Misha On

By default, Meteor publishes the current user. I see that you have a addUser template and a listUsers template. The problem is that while addUser is subscribed to the users publication, listUsers is not (this would depend on what else you have in your router of course). To fix this, change the call to this.render to render the listUsers template. Then, your users helper should work, and you can render the information however you like.

I tested this with my own app (the Microscope project from DiscoverMeteor) and it worked for me. Hope it works for you too. Comment here if not, and be sure to accept this answer if it worked. =)