efriis
efriis

Reputation: 1541

Publishing Meteor Users to Admin

I'm trying to publish all emails and "roles" to "admin" users (using the meteor-roles meteorite package), and the server knows what it's trying to publish, but for some reason the client is not picking up the data. Here's the piece of code involved:

Server Code:

Meteor.publish("directory", function() {
   if(Roles.userIsInRole(this.userId, 'admin')) {
      //next line shows that it outputs that there are 2 documents in this
      console.log(Meteor.users.find({}, {fields:{emails:1, roles:1}}).count());
      return Meteor.users.find({}, {fields:{emails:1, roles:1}});
   } else {
      return {};
   }
}

Client Code:

Meteor.subscribe("directory");
Directory = new Meteor.Collection("directory");

//and then to simulate my use for this collection
console.log(Directory.find().count());
//which outputs 0

Why isn't the client getting the documents? (and I am definitely logging in with an account with role "admin")

Thanks!

EDIT and SOLUTION!

Okay, so I figured out what I was doing wrong. I just need to publish "directory" on the server, and subscribe on the client. Then, all user data goes into the Meteor.users collection, and I shouldn't define my own "Directory=new Meteor.Collection('directory')". I can then access the data via Meteor.users. Cheers!

Server: use same code as above

Client:

Meteor.subscribe("directory");
console.log(Meteor.users.find().count()); //outputs 2, yay!

Upvotes: 3

Views: 754

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Your collection should probably be defined on the beginning so that client knows where to write!

Also, your Meteor.subscribe("directory"); runs once when the app is loaded. You should make it reactive.

Directory = new Meteor.Collection("directory");

Deps.autorun(function(){
  Meteor.subscribe("directory");
});

Upvotes: 0

Related Questions