【问题标题】:Meteor collection observe change流星收集观察变化
【发布时间】:2017-11-05 04:41:45
【问题描述】:

以下代码在集合中填充特定信息时触发客户端。有时它不能在智能手机上运行,​​但在桌面 Chrome 上运行良好。
在集合上调用findfindOne 是否正确?不是过度杀戮?谢谢

  'myMethod': function (age, health) {
    Meteor.call('doStuff-that-will-change-the-document');
    myCollection.find().observeChanges({   // <===== find
      changed: function (id, fields) {
        let newDoc = myCollection.findOne({age: age});  // <==== another findOne
        Meteor.users.update({_id: Meteor.user()._id}, {$unset: {'profile.taskInProgress': ''}});
        if (newDoc) {
          if (fields.filed1) {
            lib.dothis1(newDoc, health);
          } else if (fields.field2) {
            lib.dothat(newDoc, health);
          }
        }
      }
    });
  },

编辑

field1field2 分别更新时,我需要客户端代码触发lib.dothis1lib.dothat

【问题讨论】:

    标签: meteor


    【解决方案1】:

    如果您想捕捉添加的内容,只需使用 added 属性 - see the docs

    建立一个实时查询,当查询结果改变时调用回调。与观察相反,observeChanges 仅提供新旧结果集之间的差异,而不是更改文档的全部内容。

    回调可能具有以下功能作为属性:

    added(id, fields) or
    

    一个新文档进入了结果集。它具有 id 和字段 指定的。 fields 包含文档的所有字段,不包括 _id 字段。新文档在 before 标识的文档之前,如果 before 为空,则在末尾。

    比添加新文档时获得新的回调,这比再次使用find 更有效。

    【讨论】:

    • 我再次检查了这个问题,目前还不清楚“年龄”如何通知新文档,所以也许我误解了这个问题 - 如果是这样,请告诉我,并澄清你希望通过 @ 实现的目标987654325@.
    猜你喜欢
    • 2017-08-20
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多