Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Meteor user object in a Handlebars template function

Tags:

meteor

The following simple function on a template will usually fail on me because the user object is undefined, even though I'm logged in:

Template.hello.userData = function() {
  var user = Meteor.users.findOne(Meteor.userId());
  return user.emails[0].address;
};

It's easy to duplicate:

  • on the command-line:
    • meteor create test-user
    • cd test-user
    • meteor add accounts-password
    • meteor add accounts-ui
  • edit test-user.js and add the 'userData' code (above) into the isClient block
  • edit test-user.html and add inside 'hello' template: {{loginButtons}} {{userData}}
  • go to the app and create an account, which signs you in and you get an error in the console: Uncaught TypeError: Cannot read property 'emails' of undefined

The insidious thing is the "usually" part: it sometimes succeeds, so there may be a race condition involved... and it may be something with the order that dependencies are loaded.

I actually hit this in a more complicated app, trying to access the 'profile' property of my logged-in user (which exists, I promise). It gives slightly different errors messages in different browsers; here's the stack trace in Chrome:

Exception from Deps recompute: TypeError: Cannot read property 'profile' of undefined
    at Object.Template.workbook.owner (http://localhost:3000/client/client.js?153b3db62478692678dd9fdf9f1a9dd0b6b6a76e:130:21)
    at apply (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:276:24)
    at invoke (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:301:12)
    at http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:365:30
    at Object.Spark.labelBranch (http://localhost:3000/packages/spark.js?3a050592ceb34d6c585c70f1df11e353610be0ab:1171:14)
    at branch (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:355:20)
    at http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:364:18
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?13ab483e8a3c795d9991577e65e811cd0b827997:130:11)
    at template (http://localhost:3000/packages/handlebars.js?c2b75d49875b4cfcc7544447aad117fd81fccf3b:358:7) debug.js:41
Exception from Deps recompute: Error: Can't create second landmark in same branch
    at Object.Spark.createLandmark (http://localhost:3000/packages/spark.js?3a050592ceb34d6c585c70f1df11e353610be0ab:1226:13)
    ...<snip>...

That last error about Can't create second landmark in same branch send me on a chase through other posts that all had to do with loops, but this is just a simple collection access. There are some forEach calls in the stack trace, so I tried solutions like those in Meteor issue 281, to no avail.

Hints and explanations of Meteor conventions are much appreciated. Thanks.

like image 207
Trent Avatar asked Jan 18 '26 19:01

Trent


1 Answers

I was experiencing this same issue.

return user && user.emails[0].address;

Checking if user is undefined first ( user && ... ) seems to have done it.

like image 137
Luke Avatar answered Jan 21 '26 01:01

Luke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!