【发布时间】:2017-04-28 21:47:11
【问题描述】:
流星新手在这里。我对 Meteor 的反应感到困惑。我可以从 Mongo 控制台更新集合,它会立即更新 UI。但不是 mongo 聚合?
我正在使用 meteorhacks:aggregate 将 mongo 的 aggregate() 加载到流星中。
聚合效果很好。我可以在 mongo 控制台中立即看到数据更新。但是,如果我将其暴露给 UI,则即使在客户端刷新时也不会更新。
db.collection:
{a:1,b:2}
{a:1,b:2}
代码:
inputCollection = new Meteor.Collection('input_collection')
outputCollection = new Meteor.Collection('output_collection')
Meteor.methods({
pleaseAggregate: function() {
inputCollection.aggregate([{
$group : {
_id : "$a",
count: { $sum: 1} //should return 2 with the sample data above
}
},
{$out : "output_collection"}
]);
}
});
HTML
<p>Aggregates: {{agg.count}}</p>
客户端.js
Template.debug.helpers({
agg: function() {
return outputCollection.find().fetch()[0]
}
});
顺便说一句,它正在发布,我安装了“不安全”。
我猜我错过了一些对流星来说很明显的东西。这是什么?
【问题讨论】:
-
无论您是否使用聚合,方法调用都不是反应式的。我不清楚
AggCollection是什么...您是否期望这是汇总结果应该去的地方?如果是这样,我认为我们需要擦掉黑板并重新开始。 -
@DavidWeldon 你确定吗?我还有其他方法调用“inputCollection.remove({})”,它总是会响应式地更新 UI。为了清楚起见,我重命名了一些变量。
-
让我澄清一下 - 方法调用的 result 不是响应式的。副作用肯定是。因此,如果我调用返回文档的方法,则该调用不是响应式的。如果我调用插入文档的方法,则该操作(新文档)的结果可能会导致客户端发生反应性更新。这有意义吗?
-
计算实时聚合查询非常困难,这就是原因。
-
@DavidWeldon 谢谢大卫。如果您查看我的代码,我将输出到 mongo {$out : "output_collection"} 中的集合。这成功结束,我可以在 mongo 控制台中看到结果。但是,我可以看到数据在完全重启流星后仅出现在 UI 中。那么为什么聚合插入一个mongo文档后,和正常插入不一样呢?
标签: meteor