Meteor 观察更改在服务器上添加的回调对所有项目触发 [英] Meteor observe changes added callback on server fires on all item

查看:20
本文介绍了Meteor 观察更改在服务器上添加的回调对所有项目触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tracker.autorun(function() {
  DATA.find().observeChanges({
    added: function(id, doc) {
       console.log(doc);
    }
  });
});

正在服务器上调用此代码.每次流星服务器启动时, added 函数都会为数据库中的每个项目触发.有没有办法让 added 回调只在添加新项目时触发?

This code is being called on the server. Every time the meteor server starts, the added function fires for every single item in the database. Is there a way to have the added callback fire only when new items are added?

推荐答案

added 将在第一次运行 observeChanges 时为结果集中的每个文档调用.诀窍是在此初始化期间忽略回调.我在对 this 问题的回答中有一个扩展示例,但此代码应该为你工作:

added will be called for every document in the result set when observeChanges is first run. The trick is to ignore the callback during this initialization period. I have an expanded example in my answer to this question, but this code should work for you:

(function() {
  var initializing = true;
  DATA.find().observeChanges({
    added: function(id, doc) {
      if (!initializing) {
        console.log(doc);
      }
    }
  });
  initializing = false;
})();

请注意,Tracker.autorun 是仅限客户端的函数.在服务器上,我认为它只执行一次.

Note that Tracker.autorun is a client-only function. On the server I think it only ever executes once.

这篇关于Meteor 观察更改在服务器上添加的回调对所有项目触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆