[solved] Why does Meteor.user() return 'true' in tpl helpers?

js noob here :grin:
So if a user is logged in and I attach return Meteor.user(); to isUser helper and then add this html {{#if isUser}}, it will evaluate to true.
Shouldn’t isUser return Object?

You’re seeing the effect of “truthy” (and conversely “falsey”) values. http://james.padolsey.com/javascript/truthy-falsey/

Objects are “truthy”, in that they can be evaluated to a Boolean true. Numbers are also truthy, except when they’re equal to 0 or are NaN. Empty strings are false but non-empty strings are true.

if evaluates the truthiness of a statement. In this case, it is evaluating what is returned by Meteor.user(). If the user is logged in it does return an (non-empty) object, which is then evaluated to true. If it returned undefined it would be evaluated to false.

There’s more intricacies and gotchas to the whole topic that I’m sure more experienced developers could go on about, but this should be enough for a basic understanding.

1 Like

if the user is logged out, then if (Meteor.user()) will return false because it is undefined.

There already exists a global helper currentUser which you can use to check/access the user (in this example requiring the user to have a profile object)

{{#if currentUser}}
  {{currentUser.profile.name}}
{{/if}}
2 Likes