Top Level Fields on User Collection [Solved]

Hi All,

Im fairly new to Meteor and I am struggling to add top level fields to the users collection. I have followed the meteor guide, however, when I call the method the collection is updated. I know the method is working as when I modify it to be profile.field instead, the field is added to the collection so I am not 100% sure what the problem is.

I was wondering does anyone have a working example of adding top level fields to the user collection?

Thanks.

There may be some other ways to do it but I just initialize any extra fields when the user is first created in Accounts.onCreateUser and just do Meteor.users.update as needed thereafter.

Meteor.users has special built in security, fields won’t be published unless you specifically add them to the fields option in the publish function like this:

Meteor.publish('users', function(){
	if(this.userId){
		return Meteor.users.find({},{fields:{myfield:1}});
	}
});

(And make sure never to publish the “services” field, as it contains the password hash!)

1 Like

Thanks for the quick response! I have tried both suggestions but I still seem to have no luck.

Have uploaded a repository to GIT: https://github.com/GreatFallz/meteor_test

Which demonstrates the method working on profile.primaryAddress but not primaryAddress.

Any other tips?

where u subscribe your user collection ?

do u use auto publish ?

Yes, for the purpose of that repo I have add both autopublish and insecure

it seems like you haven’t subscribe your user publication

so it will not publish that field, but you can try

Meteor.publish('', function () {
  return Users.find({}, { fields: { services: false } })
})

and u should always hide service field

I though with autopublish that wouldnt matter?

this will override the autopublish

Thanks! that did the trick. I didn’t realise that even with autopublish you still need to specifically publish the fields.