javascript Meteor 观察更改在服务器上添加的回调对所有项目触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21355802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meteor observe changes added callback on server fires on all item
提问by Bads
Tracker.autorun(function() {
DATA.find().observeChanges({
added: function(id, doc) {
console.log(doc);
}
});
});
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
函数都会为数据库中的每个项目触发。有没有办法added
只在添加新项目时触发回调?
回答by David Weldon
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 thisquestion, but this code should work for you:
added
将在observeChanges
第一次运行时为结果集中的每个文档调用。诀窍是在此初始化期间忽略回调。我在回答这个问题时有一个扩展的例子,但这段代码应该适合你:
(function() {
var initializing = true;
DATA.find().observeChanges({
added: function(id, doc) {
if (!initializing) {
console.log(doc);
}
}
});
initializing = false;
})();
Note that Tracker.autorunis a client-only function. On the server I think it only ever executes once.
请注意,Tracker.autorun是客户端专用的功能。在服务器上,我认为它只执行一次。
回答by John Furneaux
I struggled with this for a long time. For some reason, David's answer did not work for me - it was firing after the initializing variable was set to false.
我为此挣扎了很长时间。出于某种原因,大卫的回答对我不起作用 - 它在初始化变量设置为 false 后触发。
This pattern from Avi was successful for me:
Avi 的这种模式对我来说很成功:
var usersLoaded = false;
Meteor.subscribe("profiles", function () {
// at this point all new users sent down are legitimately new ones
usersLoaded = true;
});
Meteor.users.find().observe({
added: function(user) {
if (usersLoaded) {
console.log("New user created: ", user);
}
}
});
回答by ZuzEL
Since it is initialization issue, you can do this.
由于这是初始化问题,您可以这样做。
var observerOfMessages = Messages.find({}).observe({
added: function(doc){
if(!observerOfMessages) return;
console.log(doc)
}
});
This is more elegant actually.
这实际上更优雅。
回答by santervo
Provide a selector for the query which does not match old items. If using mongo ObjectID as _id
you could query for items that have _id
greater than the latest item's:
为与旧项目不匹配的查询提供选择器。如果使用 mongo ObjectID 作为_id
您可以查询_id
大于最新项目的项目:
const latest = DATA.findOne({}, {sort: {_id: -1}})
DATA.find({_id: {$gt: latest._id}}).observeChanges({
added: function() { ...?}
})
Or with createdAt
timestamp:
或带createdAt
时间戳:
const currentTime = new Date()
DATA.find({createdAt: {$gt: currentTime}}).observeChanges({
added: function() { ...?}
})
回答by ejb
Here's another way to solve this:
这是解决此问题的另一种方法:
Meteor.subscribe('messages', function() {
var messages = Messages.find();
var msgCount = messages.count();
messages.observe({
addedAt: function(doc, atIndex) {
if(atIndex > (msgCount - 1)) console.log('added');
}
});
});
Should only fire for docs added after the existing amount is delivered. It's important that this goes in an onReady
callback for Meteor.subscribe
so that the msgCount
changes as your subscription does... if for example, you're paginating your subscriptions.
应仅针对在交付现有金额后添加的文档触发。重要的是,这会在onReady
回调中进行,Meteor.subscribe
以便msgCount
您的订阅所做的更改......例如,如果您正在对订阅进行分页。