流星:如何知道数据库准备就绪?

我想在页面加载后尽快执行Meteor集合查询.我尝试的第一件事是这样的:
Games = new Meteor.Collection("games");
if (Meteor.isClient) {
  Meteor.startup(function() {
    console.log(Games.findOne({}));
  }); 
}

这不行,虽然(它打印“未定义”).在JavaScript控制台调用时,相同的查询工作几秒钟.我认为数据库准备好之前有一些滞后.那么我怎么知道这个查询会成功?

Meteor版本0.5.7(7b1bf062b9)在OSX 10.8和Chrome 25下.

解决方法

您应该首先从服务器发布数据.
if(Meteor.isServer) {
    Meteor.publish('default_db_data',function(){
        return Games.find({});
    });
}

在客户端上,只有在从服务器加载数据之后才执行集合查询.这可以通过在订阅呼叫中使用反应会话来实现.

if (Meteor.isClient) {
  Meteor.startup(function() {
     Session.set('data_loaded',false); 
  }); 

  Meteor.subscribe('default_db_data',function(){
     //Set the reactive session as true to indicate that the data have been loaded
     Session.set('data_loaded',true); 
  });
}

现在当您执行收集查询时,您可以检查数据是否已加载:

if(Session.get('data_loaded')){
     Games.find({});
}

注意:删除autopublish包,它认将所有的数据发布给客户端,并且是不好的做法.

删除它,请从根项目目录中的每个项目执行$meteor remove autopublish.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...