我在我的 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
)查询这个,我会得到一个用户数组。
所以我想知道这段代码在客户端运行是否有问题。在同一个文件中,我让这个查询在其他地方工作,但可能在服务器上下文中。
加载模板/页面后,如何ReactiveVar
使用 Mongo 结果填充它?
Meteor.startup()
如果我在服务器端做类似的事情:
console.log(User.find({}).count());
它给了我正确数量的用户。立即地。
@编辑
如果我只添加setTimeout
几秒钟(它不能只是 1 秒,它需要更长的时间),它可以在同一个地方工作。
Template.panel.onCreated(function loginOnCreated() {
//...
setTimeout(function(){
template.timeline.set(User.find({}).fetch());
console.log(timeline)
},3000);
});
那么,有谁知道为什么要让我做这个手术需要这么长时间?任何解决方法?