繁体   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