Listing all users on client with meteor

15,911

Solution 1

if you auto publish users collection without use subscribe

if Meteor.isServer

    Meteor.publish null, ->
        Meteor.users.find {},
            fields:
                username: 1
                profile: 1

if you want to subscribe specify users you can

if Meteor.isServer

    Meteor.publish 'users-by-selector', (options) ->
        Meteor.users.find options, # options as selector like $in: name: 'john'
            fields: # use fields to only publish specify fields you want to send. 
                username: 1
                profile: 1

if Meteor.isClient

    Meteor.autosubscribe ->
        options = Session.get 'your mongodb selector'
        Meteor.subscribe 'users-by-selector', options, ->
            console.log 'on Subscribe Complete Callback.'
            if Meteor.users.find().count()
                Session.set 'specifyUsersLoaded', true

Solution 2

Here is an excerpt from Meteor's Parties example:

// in server.js
Meteor.publish("directory", function () {
  return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});

// in client.js
Meteor.subscribe("directory");
Share:
15,911
danielsvane
Author by

danielsvane

Updated on June 18, 2022

Comments

  • danielsvane
    danielsvane almost 2 years

    According the the meteor documentation, all users should be published to all clients if the autopublish package is installed.

    http://docs.meteor.com/#meteor_users

    I have the autopublish package installed, but using forEach on Meteor.users only lists the currently logged in user.

    Is there a more correct way to list all the users on the client by using coffeescript?

  • danielsvane
    danielsvane over 11 years
    Your first snippet works, though I had to add emails: 1 under fields:, as both username and profile are empty for all the users. Apparently a user isn't autopublished unless there is a value in its autopublished fields.
  • rdickert
    rdickert over 10 years
    I did not find the limitation mentioned by @danielsvane. One thing to pay attention to, though, is that different provider packages provide different data structures. This makes sense when you think about it, but it can be confusing if you aren't ready for it. @crapthings' code above (including profile and username) would work well to get usernames for both Facebook and the accounts-password packages. To get the user name from a user record, you would need user.username || user.profile.name.
  • Bjorn
    Bjorn over 10 years
    Great and helpful answer +1, but -1 for assuming CoffeeScript. Question is not tagged as CoffeeScript. It's not called Meteor.coffee
  • danielsvane
    danielsvane over 10 years
    @rdickert They might have fixed that little hurdle, as this question is kinda old.
  • danielsvane
    danielsvane over 10 years
    @BjornTipling I was using CoffeeScript though, so his answer was spot on.
  • Miro
    Miro over 10 years
    @danielsvane Where exactly did you use CoffeeScript in your question?
  • Erdal G.
    Erdal G. over 8 years
    @danielsvane Questions-Answers in SO are for everybody. So if your question is not Coffeescript, we are not waiting for it :) (I'm saying this drinking coffee... sad world)
  • Barry Michael Doyle
    Barry Michael Doyle over 8 years
    Could someone post a non Coffeescript version of the answer?
  • Barry Michael Doyle
    Barry Michael Doyle over 8 years
    The example isn't there anymore, how can I iterate each user in a template?
  • Amir Nissim
    Amir Nissim over 8 years
    here it is: github.com/meteor/meteor/tree/devel/examples/other/parties (code is the same, updated link)