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:
Uncaught TypeError: Cannot read property 'emails' of undefinedThe 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.
I was experiencing this same issue.
return user && user.emails[0].address;
Checking if user is undefined first ( user && ... ) seems to have done it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With