Meteor - How can I limit which fields are published to the client?

I want to publish only a limited amount of data to the client.

I tried to do it like this:

# server Meteor.publish('users', -> Meteor.users.find({}, fields: services: 0 ) ) 

But the client still gets the whole object.

 # client Meteor.startup( -> Meteor.subscribe('users') ) # ... # in another function Meteor.users.find().observe( -> changed: (updated) -> console.log updated ) 

What am I doing wrong?

+8
meteor
source share
2 answers
 Meteor.publish '', -> Posts.find({}, { fields: { title: 1, content: true, secret: false } }); 

how to add these {}

+5
source share

The code below works for me (coffeescript). The pwd field is not published.

Server

 Meteor.publish "users", (userId) -> user = Users.find userId, fields: pwd: false return user 

Client

 Meteor.autosubscribe -> userId = Session.get SESSION_USER Meteor.subscribe 'users', userId 

The only differences that I see are

  • 0 vs false ... (should only taste)
  • Access your collection through Meteor
  • In the client, my subscription is placed inside the autosubscribe when you use the observe method.

Are there any fields resulting from the result of Meteor.users.find().fetch() in the browser console?

+4
source share

All Articles