I create custom user fields
user = Meteor.users.insert({username:'Neo', money: 200})
in client i want
Tracker.autorun =>
user = Meteor.user()
user.username # "Neo"
user.money # undefined
money
is undefined, not 200
I create custom user fields
user = Meteor.users.insert({username:'Neo', money: 200})
in client i want
Tracker.autorun =>
user = Meteor.user()
user.username # "Neo"
user.money # undefined
money
is undefined, not 200
solution:
Meteor.publish null, ->
return Meteor.users.find({_id: @userId}, {fields: {money: 1}})
you must create an auto-publication with the name null, you do not need to subscribe to such publications, and they are always available on the client.
P.S. this not working:
Accounts.addAutopublishFields
forLoggedInUser: ['money']
I write this simple working function:
Accounts.publishFields = function(options) {
var currentUser, otherUsers;
({currentUser, otherUsers} = options);
if (currentUser && Object.keys(currentUser).length) {
Meteor.publish(null, function() {
return Meteor.users.find({
_id: this.userId
}, {
fields: currentUser
});
});
}
if (otherUsers && Object.keys(otherUsers).length) {
Meteor.publish(null, function() {
return Meteor.users.find({}, {
fields: otherUsers
});
});
}
};
usage:
Accounts.publishFields({
currentUser: {
steamId: 1
},
otherUsers: {
avatar: 1,
nickname: 1
}
});
I don’t think you need any special case for this. You should have publications that hold the data you need in your app.
If you always display the current user’s money, then you would just create a publication that provides that, and subscribe to it where you display it, the same as any other publication.
I do not need a subscription to the publication, I need Meteor.user()
to return all the fields I need.
You do if you are doing the request from the client.