2014-09-25 69 views
1

我有發佈方法依賴於用戶狀態:發佈這取決於用戶狀態

Meteor.publish('myGroup', function() { 
    if (this.userId != null) { 
    var user = Meteor.users.findOne(this.userId); 
    return Group.find(user.profile.groupId); 
    } else { 
    return []; 
} 
}); 

當用戶狀態變化(user.profile.groupId),我沒有得到新的數據。我可以通過刷新瀏覽器來修復它。

我試圖解決這個問題,通過使認購反應:

Tracker.autorun(function() { 
    var user = Meteor.user(); // depend on user 
    if (user != null) { 
    Meteor.subscribe('myGroup'); 
    } 
}); 

但它似乎並沒有工作。解決這個問題的最好方法是什麼?

+0

你可以看到你的自動運行功能實際上是對用戶改變運行? – Shaded 2014-09-25 15:31:40

+0

是的,自動運行在用戶更改上運行。實際上,經過測試,此代碼按預期工作。 – Jakozaur 2014-09-27 22:37:37

回答

0

傳遞未使用的參數似乎是最好的解決方法:

Meteor.subscribe("myGroup", user.profile.groupId); 
+0

由於客戶端決定組標識,因此不安全。這不是推薦的。 – kahonmlg 2015-02-21 09:03:30

+0

服務器根本不使用此參數。雖然我同意你需要小心user.profile的允許/拒絕規則。 – Jakozaur 2015-02-25 01:45:51