铁:路由器永远不会返回定义的集合(版本1.0.12)

时间:2016-02-03 19:11:11

标签: javascript meteor iron-router

出于某种原因,我的data: function...始终返回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()
  }
 });

只看到:

enter image description here

这是不起作用的?

1 个答案:

答案 0 :(得分:2)

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

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

另一方面,原因是在某些情况下,您可能希望集合只能在客户端上访问,或者只能在服务器上访问,但不能同时访问两者。