Ödemis
Ödemis

Reputation: 21

Publishing All users in meteor with enabled accounts package and disabled autopublish

I want publish all users collection to all my clients. I have removed the autopublish package and added the accounts package to the project. I know that the accounts package only publish the user collection to the logged in user, but i want send the complete users-collection.

Pub/Sub Code:

if (Meteor.isServer) {
  Meteor.publish('users', function() {
    return Meteor.users.find();
  });
}
if (Meteor.isClient) {
  Meteor.subscribe('users');
}

And here are the template that want present all users from mongodb:

Template.userList.helpers({
  users: function () {
    return Meteor.users.find(); 
  }
});

Upvotes: 0

Views: 164

Answers (2)

nicolsondsouza
nicolsondsouza

Reputation: 426

if (Meteor.isServer) {

 Meteor.publish(null, function() {
      return Meteor.users.find();
 });

}

This is appropriate way without even subscribing, it will publish all the user collectioin, but this isn't advisable.

Upvotes: 0

Ödemis
Ödemis

Reputation: 21

Following setup: i have created meteor project and deleted the autopublish and insecure package and added the accounts-password package to my project.

Pub/Sub Code :

if (Meteor.isServer) {
  Meteor.publish('usersData', function() {
    return Meteor.users.find();
  });
}
if (Meteor.isClient) {
  Meteor.subscribe('usersData');
}

Templates:

<template name="userList">
    <h1> Users </h1>
    <ul>
      {{#each usersList}}
        {{> userItem}}
      {{/each}}
    </ul>
</template>

<template name="userItem">
  <li>{{username}}</li>
</template>

Template helper:

Template.userList.helpers({
  usersList: function () {
    return Meteor.users.find(); 
  }
});

Works.

Please refrain using a meteor predefined variable name such as users and avoid confusion during debugging process

Upvotes: 2

Related Questions