我很抱歉这个模糊的标题和长篇文章,但我很难描述我得到的行为。我会尝试任何一种方式来解释。
我有一个新闻源页面,我正在显示帖子。我只想显示比用户打开页面时更旧的帖子,这样列表就会保持被动,而不会在顶部添加新帖子。所以我决定在发布函数中使用observeChanges,如下所示:
Newsfeed = new Mongo.Collection('newsfeed');
if (Meteor.isServer) {
Meteor.publish("newsfeed", function (init_last_activity, limit) {
var self = this;
check(limit, Number);
check(init_last_activity, Date);
var init = true,
count = 0;
var handle = Newsfeed.find({},{
'limit': limit,
'sort': {'updated_time': -1}
}).observeChanges({
// when a post is added, we check if it's got an older
// updated_time than the initial updated time field
// generated on init and only if so, we display
// the post to the client
added: function(id, doc) {
count++;
console.log('++++ POST NR. ' + count + ' ++++ ('+id+')');
console.log('Updated time: ', doc.updated_time);
console.log('Last activity: ', init_last_activity);
if (doc.updated_time < init_last_activity) {
console.log('- - - - -');
console.log('ADDED!');
self.added('newsfeed', id, doc);
console.log('ID: ' + id);
console.log(' ');
}
},
changed: function(id, fields) {
console.log('Trigger changed for id:', id);
self.changed('newsfeed', id, fields);
}
});
init = false;
this.ready();
this.onStop(function() {
handle.stop();
});
});
}
包含控制台日志,以说明实际上正在调用self.added()
。以下是用户刷新页面时的日志:
I20141024-20:21:08.886(-7)? ++++ POST NR. 1 ++++ (9WMnDt5tJ6wePwfcs)
I20141024-20:21:08.887(-7)? Updated time: Fri Oct 24 2014 20:17:47 GMT-0700 (PDT)
I20141024-20:21:08.887(-7)? Last activity: Fri Oct 24 2014 20:21:08 GMT-0700 (PDT)
I20141024-20:21:08.887(-7)? - - - - -
I20141024-20:21:08.887(-7)? ADDED!
I20141024-20:21:08.887(-7)? ID: 9WMnDt5tJ6wePwfcs
I20141024-20:21:08.887(-7)?
I20141024-20:21:08.887(-7)? ++++ POST NR. 2 ++++ (7pt3nBKqgCKJ4G5fc)
I20141024-20:21:08.887(-7)? Updated time: Fri Oct 24 2014 20:20:59 GMT-0700 (PDT)
I20141024-20:21:08.888(-7)? Last activity: Fri Oct 24 2014 20:21:08 GMT-0700 (PDT)
I20141024-20:21:08.888(-7)? - - - - -
I20141024-20:21:08.888(-7)? ADDED!
I20141024-20:21:08.888(-7)? ID: 7pt3nBKqgCKJ4G5fc
I20141024-20:21:08.888(-7)?
如您所见,已向客户发送了两个帖子。现在,当同一个用户尝试“喜欢”第二篇帖子(ID: 7pt3nBKqgCKJ4G5fc
)时,几秒钟之后,这会在上面的代码中触发self.changed()
,这种情况会发生:
I20141024-20:21:13.997(-7)? Trigger changed for id: 7pt3nBKqgCKJ4G5fc
I20141024-20:21:13.997(-7)? Exception in queued task: Error: Could not find element with id 7pt3nBKqgCKJ4G5fc to change
I20141024-20:21:13.997(-7)? at _.extend.changed (packages/ddp/livedata_server.js:185)
I20141024-20:21:13.997(-7)? at _.extend.changed (packages/ddp/livedata_server.js:397)
I20141024-20:21:13.997(-7)? at _.extend.changed (packages/ddp/livedata_server.js:1154)
I20141024-20:21:13.997(-7)? at Newsfeed.find.observeChanges.changed (app/collections/newsfeed.js:125:22)
I20141024-20:21:13.998(-7)? at packages/mongo/observe_multiplex.js:163
I20141024-20:21:13.998(-7)? at Array.forEach (native)
I20141024-20:21:13.998(-7)? at Function._.each._.forEach (packages/underscore/underscore.js:105)
I20141024-20:21:13.998(-7)? at Object.task (packages/mongo/observe_multiplex.js:157)
I20141024-20:21:13.998(-7)? at _.extend._run (packages/meteor/fiber_helpers.js:145)
I20141024-20:21:13.998(-7)? at packages/meteor/fiber_helpers.js:123
几秒钟之前附加到self.added()
函数的完全相同的ID。奇怪的是,如果我跳过if语句并且只允许self.added()
运行,无论如何,一切正常:
added: function(id, doc) {
self.added('newsfeed', id, doc);
}
所以,虽然在前面的例子中调用self.added()
,但似乎只是因为它在if语句中被调用会破坏代码。
有没有人知道为什么会这样?我是否误解了添加和更改的功能?我很困惑:))