【发布时间】:2016-11-22 05:26:23
【问题描述】:
我在我的 Meteor 项目中获得此代码,在 client/main.js 文件中
Template.panel.onCreated(function loginOnCreated() {
var profile = Session.get('profile');
this.myvar = new ReactiveVar(User.find({}).fetch());
});
而User.find({}) 的结果为空。如果我在其他任何地方查询这个(包括meteor mongo),我会得到一个用户数组。
所以我想知道这段代码在客户端运行是否有问题。在同一个文件中,我让这个查询在其他地方工作,但可能在服务器上下文中。
如何在加载模板/页面后立即使用 Mongo 结果填充此 ReactiveVar?
如果我在服务器端做类似Meteor.startup() 的事情:
console.log(User.find({}).count());
它给了我正确的用户数量。马上。
@edit
如果我只是添加几秒钟的setTimeout(它不能只是 1 秒,它需要更长的时间),它在同一个地方工作。
Template.panel.onCreated(function loginOnCreated() {
//...
setTimeout(function(){
template.timeline.set(User.find({}).fetch());
console.log(timeline)
},3000);
});
那么,有谁知道为什么要让我做这个手术需要这么长时间吗?任何解决方法?
【问题讨论】:
标签: mongodb meteor reactive-programming meteor-methods