Aviad Ben Dov
Aviad Ben Dov

Reputation: 6409

Publishing all Meteor.users doesn't work

I'm trying to publish all the usernames to the clients, even if not signed in. For that, on the server I have:

Meteor.publish("users", function() { 
   return Meteor.users.find({}, {fields : { username : 1 } });
});

And on the client:

Meteor.subscribe("users");

However, when I try to access the Meteor.users collection, I find nothing there.

(This is essentially the same as the question here: Listing of all users in the users collection not working first time with meteor js, only without checking the roles for admin first. Still doesn't seem to work..)

I'm probably missing something silly..

Upvotes: 1

Views: 342

Answers (2)

Alex Alexeev
Alex Alexeev

Reputation: 754

This code outputs all the usernames to the clients, even if not signed in (in this case on /users page):

server/publications.js:

Meteor.publish("userlist", function () {
    return Meteor.users.find({},{fields:{username:1}});
});

client/users_list.js:

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

client/users_list.html:

<template name="usersList">
      {{#each users}}
        {{username}}
      {{/each}}
</template>

lib/router.js (using iron:router package):

Router.route('/users', {
    name: 'usersList',
    waitOn: function(){
        return Meteor.subscribe("userlist");
    }
});

Hope it helps.

Upvotes: 1

Ethaan
Ethaan

Reputation: 11376

I find the same issue, and after doing a research i find this package, i think it may should help you.

Take a look and hope it help you

Update

First move the subscription to the /lib folder, just to make sure its the first thing meteor do when start, also change a little bit the subscription like this on the /lib, folder.

Tracker.autorun(function() {
if(Meteor.isClient) {
  if (!Meteor.user()) {
    console.log("sorry you need to be logged in to subscribe this collection")
   }else{
    Meteor.subscribe('users');
   }
 } 
});

For better security we just subscribe to the users collection when the client its logged in

Upvotes: 1

Related Questions