1
votes

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..

2
Where do you try to access this cursor, on the client? - Kyll
Are you sure that the subscription is ready when you are accessing the data? - stubailo
@stubailo should it matter? Shouldn't the reactive model update my data when it is ready? - Aviad Ben Dov
Then perhaps you're doing something wrong in the template where you're showing all the users. - Peppe L-G
@PeppeL-G Nothing to do with the template. count() returns 0. - Aviad Ben Dov

2 Answers

1
votes

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

1
votes

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.