Iron:router 从不 Returns 定义集合(版本 1.0.12)

Iron:router Never Returns Defined Collection (version 1.0.12)

出于某种原因,我的 data: function... 总是 returns 为 undefined

这是我的服务器代码:

Flyers = new Mongo.Collection('flyers');

Meteor.publish('flyers', function() {
  return Flyers.find({});
});

越简单越好。

这是我的路线文件(位于名为 both 的目录中):

Router.route('/dashboard', {
  template: 'dashboard',
  layoutTemplate: "dashboardLayout",
  loadingTemplate: 'loading',
  waitOn: function() {
    return Meteor.subscribe('flyers');
  },
  data: function() {
    return Flyers.find()
  }
 });

只看到:

为什么这不起作用?

由于您的 Flyers 变量是用您的服务器代码定义的,因此它只能在服务器上访问。尝试在客户端访问此变量将导致 undefined 值,因为它不存在。

客户端和服务器都需要的公共代码应该在 lib 文件夹中定义,以便在两个地方都可以访问。

附带说明一下,这样做的原因是在某些情况下,您可能希望集合只能在客户端或服务器上访问,但不能同时在两者上访问。