簡體   English   中英

訂閱Meteor.users集合更新

[英]Subscribe to Meteor.users collection updates

我在應用程序中使用react-komposer和React,有一次我更改了反映當前用戶的Meteor.users集合項的用戶profile屬性。 這里的問題在於,它在調用render不會反應性地回送。 我對Meteor.users集合所做的任何操作,即使我在render函數中引用了Meteor.user() ,也不會重新渲染該組件。 如果我做

render() {
  console.log('rendered');

  /// ...
}

那么我只能在控制台上看到兩次“渲染”:最初渲染了該組件,第二次准備了其他訂閱。

我想訂閱Meteor.users集合,但我不知道有沒有辦法。

我絕對可以使用Tracker對每個Tracker循環做出反應,但對我來說感覺太過1.2。 但這是唯一的方法嗎?

另外,我肯定也讀過其他有關Meteor.users &A的問題, Meteor.users這樣一個這個用戶,將用戶集合包裝到pub / sub中的想法直接感覺很不自然,並且幾乎不是監聽集合中每個更新的好方法。

您必須在服務器代碼上發布有關meteor.users集合的發布。

    Meteor.publish('allUsers', function allUsers() {
      return Meteor.users.find({}, {
        fields: {
          services: 0,
        },
     });
   });

然后,您可以在容器上訂閱該發布,並查詢Meteor.users集合並將該結果發送到組件。

const composer = (props, onData) => {
const userHandle = Meteor.subscribe('allUsers');
if (userHandle.ready()) {
    const users = Meteor.users.find({}).fetch();
    onData(null, {
        users,
    });
}
};
export default composeWithTracker(composer)(YourComponent);

之后,對YourComponent內部集合所做的任何更改都將反映在客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM