[英]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.