0

我在我的 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);
});

那么,有谁知道为什么要让我做这个手术需要这么长时间?任何解决方法?

4

1 回答 1

0

User.find({}).fetch() 将仅提供服务器端的用户列表。

您可能可以编写一个meteor 方法来获取服务器端的用户列表,并使用meteor.call 给它调用。

在此调用的回调函数中,您可以将结果分配给所需的变量。

于 2016-11-22T05:39:32.897 回答