从渲染事件访问时收集为空?

我有一个我正在订阅的集合,但是当我尝试从我的onRendered事件访问它时,它总是以空数组的forms返回。 以下是我正在使用的方法:

 FlightCounts = new Mongo.Collection("flightCounts"); if (Meteor.isClient) { Meteor.subscribe('flightCounts'); Template.hello.rendered = function(){ var counts = FlightCounts.find().fetch() console.log(counts) } } if (Meteor.isServer) { Meteor.startup(function () { // code to run on server at startup Meteor.publish('flightCounts', function(){ return flightCounts.find(); }) }); } 

任何人都可以看到为什么我的collections将永远是空的吗? 任何build议如何得到它填充?

根本问题是发布函数应引用meteor的Mongo.Collection名称, FlightCounts ,而不是原始的db.collection名称flightCounts

 Meteor.publish('flightCounts', function(){ return FlightCounts.find(); }); 

我也同意先前的答案,您的模板应该检查,以确保订阅logging数据前,否则它可能还没有到达:

 Template.hello.onRendered(function(){ this.autorun(() => { let ready = Template.instance().subscriptionsReady(); if (!ready){ return; } let counts = FlightCounts.find().fetch(); console.log(counts); }); }); 

(请注意, Template.name.rendered的新语法是Template.name.onRendered() 。)

 if (Meteor.isClient) { Meteor.subscribe('flightCounts'); Template.hello.rendered = function() { var template = this; template.autorun(function () { var counts = FlightCounts.find().fetch() console.log(counts) }); } } 

您的collections没有填充,因为它仍然从服务器获取数据。 所以你应该把你的代码封装在一个自动运行中,以便在数据更新时重新运行。

PS:我在手机上,格式可能不正确。 对不起。

或者,您可以使用FlowRouterwaitOn订阅将订阅放在那里

更新令人惊讶的是, IronRouter现在也有subscriptions选项