【问题标题】:Meteor.publish doesn't update the client when an array is used in the query在查询中使用数组时,Meteor.publish 不会更新客户端
【发布时间】:2012-12-06 17:27:35
【问题描述】:

考虑这段代码:

Meteor.publish("Children", function (id) {
  // Find all the items that are children of an item that I am sharing
  //
  var sharing = Items.find({$and : [{_id:id},{sharing:{$elemMatch: {user_id:this.userId}}}]}).fetch();
  var shared_children = [];
  sharing.forEach(function(share){
    share.children.forEach(function(child){
      shared_children.push(child.id);
    });
  });
  return Items.find({_id:{$in : shared_children}});
});

在我的 Meteor.publish 中,我正在动态生成一个 id 数组以在我的 .find 中使用。它适用于手动查询数据,但是当我向“共享”字段添加新元素时,只有我添加该字段的客户端显示更新。查看相同元素的其他客户端不会获得从服务器发送的更新值。他们只是用他们的 minimongo 数据库中已有的内容进行更新。服务器端MongoDB 确实显示了新条目,但我无法在创建它的客户端以外的任何客户端上查询它。

问题是我的.find 使用了一个计算值数组,因此依赖系统没有再次调用我的发布事件吗?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    Meteor 的 reactivty 运行在服务器端是一个常见的误解。相反,典型的模式是客户端发生了一些变化,它有一个反应变量(例如 Session 对象)作为订阅的参数。订阅值的更改将导致使用新参数重新调用发布。

    相反,如果你想在服务器端做这个,你需要设置一个观察者,如counts-by-room example所示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 2023-03-16
      • 2018-02-09
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多