简体   繁体   English

Deps.autorun与Firefox和IE上的Meteor.user()问题

[英]Deps.autorun with Meteor.user() issue on firefox and IE

So I have this code here for auto-subscribing to the current game of a player 所以我这里有这段代码可以自动订阅玩家当前的游戏

Deps.autorun ->
  Meteor.subscribe "game", Meteor.user().profile.game

But it only works on google chrome. 但它仅适用于谷歌浏览器。 Both firefox and IE shows errors saying that Meteor.user(...) is undefined. Firefox和IE均显示错误消息,提示未定义Meteor.user(...)。

Notice that when I type Meteor.user().profile.game directly in the console, it returns the current game id just well. 请注意,当我直接在控制台中键入Meteor.user().profile.game ,它将恰好返回当前游戏ID。 So apparently, the issue is only with the code above for some reason. 显然,出于某种原因,问题仅在于上面的代码。 Also, the other Deps.autorun functions that depends on a Session works just fine. 另外,依赖于Session的其他Deps.autorun函数也可以正常工作。 Thanks. 谢谢。

It's a race condition. 这是比赛条件。 You are assuming that the user is already logged in when the autorun executes. 您假设执行autorun时用户已经登录。 If he/she isn't, then Meteor.user() will return null . 如果不是,那么Meteor.user()将返回null One solution is to just use a soak: 一种解决方案是仅使用浸泡:

Tracker.autorun ->
  Meteor.subscribe 'game', Meteor.user()?.profile.game

But the publish function knows which user is trying to subscribe, so I'd argue that a better solution would be something like: 但是publish函数知道哪个用户正在尝试订阅,因此我认为更好的解决方案是:

Tracker.autorun ->
  if Meteor.user()
    Meteor.subscribe 'game'

Note the check for Meteor.user() in the autorun will force it to rerun when the user logs in (this is also good for performance reasons). 请注意, autorunMeteor.user()的检查将在用户登录时强制其重新运行(出于性能原因,这也是很好的选择)。 Then on the server you can do: 然后,您可以在服务器上执行以下操作:

Meteor.publish 'game', ->
  user = Meteor.users.findOne @userId
  {game} = user.profile
  Games.find game

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM