pizzarob
pizzarob

Reputation: 12059

Get Multiple Users Data on Client Meteor

So I want to be able to get all the user _id's and username's to be used in my template. I am creating a publication in server/publications.js which looks like so:

Meteor.publish('allUserData', function(){
   Meteor.users.find({}, { fields : {
       username : 1,
       _id : 1
   }});
});

But I can't for the life of me get all the user data. I am only still able to get the user data for the current logged in user. I have tried subscribing in different places; main.js, in my route, and in my collection js file.

I've tried subscribing and then calling the data in the subscription callback, but that didn't work either.

So I am wondering, am I going about this the right way? Where do I put my subscription? And why isn't it working like I would like it to? I am on day 2 of learning meteor so any help would be appreciated.

Upvotes: 0

Views: 139

Answers (2)

illuminaut
illuminaut

Reputation: 320

You are not returning any cursor in your publish function. When you do that, Meteor assumes you are using the low-level added/changed/removed interface, which isn't what you're after here. Change your function to return the cursor from find():

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

(The _id: 1 is not necessary btw) Then in your client subscribe in the appropriate place (where that is depends), for example in the autorun:

Tracker.autorun(function () {
    Meteor.subscribe("allUserData");
});

Upvotes: 1

David Weldon
David Weldon

Reputation: 64342

You are missing the return:

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

Also note that you don't need to specify the _id in fields, as it's added automatically.

Upvotes: 1

Related Questions