[英]Deps.autorun with Meteor.user() issue on firefox and IE
所以我这里有这段代码可以自动订阅玩家当前的游戏
Deps.autorun ->
Meteor.subscribe "game", Meteor.user().profile.game
但它仅适用于谷歌浏览器。 Firefox和IE均显示错误消息,提示未定义Meteor.user(...)。
请注意,当我直接在控制台中键入Meteor.user().profile.game
,它将恰好返回当前游戏ID。 显然,出于某种原因,问题仅在于上面的代码。 另外,依赖于Session的其他Deps.autorun函数也可以正常工作。 谢谢。
这是比赛条件。 您假设执行autorun
时用户已经登录。 如果不是,那么Meteor.user()
将返回null
。 一种解决方案是仅使用浸泡:
Tracker.autorun ->
Meteor.subscribe 'game', Meteor.user()?.profile.game
但是publish函数知道哪个用户正在尝试订阅,因此我认为更好的解决方案是:
Tracker.autorun ->
if Meteor.user()
Meteor.subscribe 'game'
请注意, autorun
对Meteor.user()
的检查将在用户登录时强制其重新运行(出于性能原因,这也是很好的选择)。 然后,您可以在服务器上执行以下操作:
Meteor.publish 'game', ->
user = Meteor.users.findOne @userId
{game} = user.profile
Games.find game
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.