我正在尝试cursor.observe({added})
根据插入的字段来计算新字段。如果观察 on ,使用Docs.update()
添加的文档会中断added
,Meteor 似乎在内爆之前停止循环。但是,更新changed
作品。
为什么?我将如何计算插入时的新字段?
看评论:
Meteor.startup(function () {
var cursor = Docs.find();
var handle = cursor.observe({
added: function (doc) {
// This breaks Meteor. Meteor gets stuck in a loop and breaks.
Docs.update(
doc._id,
{$set: {metric: getCalculatedMetric( doc.x, doc.y )}}
);
// However, this would log once as expected.
// console.log(doc.name + ' has been added.');
},
changed: function (doc, oldDoc) {
// This works as expected, updates myField.
Docs.update(
doc._id,
{$set: {metric: getCalculatedMetric( doc.x, doc.y )}}
);
}
});
});