【发布时间】:2014-08-26 19:36:07
【问题描述】:
我有这个模板助手
Template.foo.helpers
'bar':->
console.log(Meteor.user().profile)
'baz':->
console.log(Meteor.user().profile)
'buzz':->
console.log(Meteor.user().profile)
它完美地记录了用户对象。
但是当我尝试在模板助手之外使用它时,它会返回一个Uncaught TypeError: Cannot read property 'profile' of undefined
这样
checkProfile=(profile)->
console.log profile
Template.foo.helpers
'bar':checkProfile(Meteor.user().profile)
'baz':checkProfile(Meteor.user().profile)
'buzz':checkProfile(Meteor.user().profile)
我认为它在加载 Meteor.user() 之前执行该函数,但是当我尝试在 checkProfile 中记录 Meteor.userId 时,例如:
checkProfile=(profile)->
console.log Meteor.userId()
console.log profile
甚至
checkProfile=(profile)->
console.log Meteor.userId()
console.log(Meteor.user())
它会尽快记录用户 ID,但仍会返回未捕获的错误。
现在我得出结论,Meteor.userId() 在流星开始时始终可用,而Meteor.user() 对象在之后加载?
请识字我的这个结论,因为这些小事情将来可能会成为我代码中的一个大问题。谢谢。
旁注:
我确定用户已登录,因为我正在测试用户登录时的错误测试。只是当页面刷新时,代码运行并在尝试运行 Meteor.user() 时返回该错误
【问题讨论】:
标签: meteor