This might be more of a MongoDB question than Meteor but I can’t seem to figure it out. I want to be able to get the value of a field with a specific ID, I know the value is a Boolean so I just want to write a function that fetches the value of a field called completedOnboarding and fetch that field value from the currently logged in user Meteor.user().
find returns a cursor from which you can fetch the result from (as an array), so you need to do find(selector, options).fetch()[0].completedOnboarding, or more simply use findOne instead: findOne(selector, options).completedOnboarding.
Also, you might want to use Meteor.userId() to avoid having to fetch the entire user document when all you care about is the _id field: http://docs.meteor.com/#/full/meteor_userid
var
completedOnboarding,
user = Meteor.users.findOne(Meteor.userId(), {fields: {'completedOnboarding': 1}})
;
if (user)
completedOnboarding = user.completedOnboarding;
findOne fetches one object matching the selector
Meteor.userId() provides a quick and lean way to get the current user’s id being at the same time an optimal unequivocal selector (no need to use {_id: Meteor.userId()} if you’re loking for the document’s _id)
specifying fields among the query options allows for an even quicker fetch of the requested document, and this can somethime make the difference if you’re looking for performance
Thanks for all the tips! I was struggling with this all day, but was just getting undefined, and then realised that I had to specifically publish the extra field I was adding on Accounts.onCreateUser() namely completedOnboarding. Pretty new with Meteor so this just slipped my mind and the fact that with the Users collection created by Meteor.loginWithTwitter() and Accounts.createUser() the only thing that’s autopublished is profile: {}.