【问题标题】:Why is my published data not reactive? [duplicate]为什么我发布的数据没有反应性? [复制]
【发布时间】:2015-12-26 06:21:58
【问题描述】:

我正在学习 Meteor,我想知道为什么我的数据在按如下方式发布时没有反应:

Meteor.publish("users", function (userId) {
    // teams the user is member of
    var teams = _.pluck(Teams.find({members:userId}).fetch(), '_id');
    // users from all those teams
    var userIds = _.union.apply(_,_.pluck(Teams.find({_id:{$in:teams}}).fetch(), 'members'));
    // user cursor
    return Meteor.users.find({_id:{$in:userIds}},{fields: {_id:1, profile: 1, emails:1}});
});

显然,这是可行的

Meteor.publish("users", function (userId) {
    return Meteor.users.find();
});

我认为额外的步骤会破坏反应性,但我该如何解决呢?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    Meteor.publish() 返回的光标只有反应性

    所以在你的返回函数中,Meteor 只观察 Meteor.users.find 光标。就是这条线……

    return Meteor.users.find({_id:{$in:userIds}},{fields: {_id:1, profile: 1, emails:1}});
    

    除非再次运行发布,否则此查询中的数组 userIds 将保持相同的值。

    当新的 userId 传递到发布时,发布会再次运行。 Teams.find 查询运行并保存新值。但是这个查询的结果保存在一个变量中 - teams。该变量是非反应性的,因此,从发布返回的游标认为它不应再次运行。

    第一次发布发布时,您将获得结果。但是当它再次运行时,游标将不会查询数据库。

    我建议在 Observe in a Publish Function 上观看 Chris Mather 的视频,并阅读客户端上的响应式连接。

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多