I want to publish only a limited amount of data to the client.
I've tried to do it like this:
# server
Meteor.publish('users', ->
Meteor.users.find({},
fields:
services: 0
)
)
But the client still receives 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?
In Meteor a publication is a named API on the server that constructs a set of data to send to a client. A client initiates a subscription which connects to a publication, and receives that data.
The Meteor. publish function is used on the server to define what data should be available to users of the application. The Meteor. subscribe function is used on the client to retrieve the data that's published from the server.
In a Meteor app, things are pretty simple: Code that runs on the server can be trusted. Everything else: code that runs on the client, data sent through Method and publication arguments, etc, can't be trusted.
Meteor.publish '', ->
Posts.find({}, { fields: { title: 1, content: true, secret: false } });
what about add those {}
The code below works for me (coffeescript). The pwd
field isn't published.
Meteor.publish "users", (userId) ->
user = Users.find userId,
fields:
pwd: false
return user
Meteor.autosubscribe ->
userId = Session.get SESSION_USER
Meteor.subscribe 'users', userId
The only differences I see are
0
vs false
... (should be a matter of taste, only)Meteor
autosubscribe
callback while you're using the observe
method.Do the fields exists in the result of Meteor.users.find().fetch()
in the browsers console, too?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With