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");
Author by
danielsvane
Updated on June 18, 2022Comments
-
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
onMeteor.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 over 11 yearsYour first snippet works, though I had to add
emails: 1
underfields:
, as bothusername
andprofile
are empty for all the users. Apparently a user isn't autopublished unless there is a value in its autopublished fields. -
rdickert over 10 yearsI 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
andusername
) would work well to get usernames for both Facebook and theaccounts-password
packages. To get the user name from auser
record, you would needuser.username || user.profile.name
. -
Bjorn over 10 yearsGreat and helpful answer +1, but -1 for assuming CoffeeScript. Question is not tagged as CoffeeScript. It's not called Meteor.coffee
-
danielsvane over 10 years@rdickert They might have fixed that little hurdle, as this question is kinda old.
-
danielsvane over 10 years@BjornTipling I was using CoffeeScript though, so his answer was spot on.
-
Miro over 10 years@danielsvane Where exactly did you use CoffeeScript in your question?
-
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 over 8 yearsCould someone post a non Coffeescript version of the answer?
-
Barry Michael Doyle over 8 yearsThe example isn't there anymore, how can I iterate each user in a template?
-
Amir Nissim over 8 yearshere it is: github.com/meteor/meteor/tree/devel/examples/other/parties (code is the same, updated link)