Search code examples
javascriptmeteoriron-router

Iron:router Never Returns Defined Collection (version 1.0.12)


For some reason, my data: function... always returns as undefined.

Here's my server code:

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

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

As simple as it gets.

Here's my routes file (which is in a directory called both):

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

Only to see:

enter image description here

Any reason this isn't working?


Solution

  • Since your Flyers variable is defined with your server code, it will only be accessible on the server. Attempting to access this variable on the client side will result in an undefined value, because it doesn't exist.

    Common code that's needed on both the client and server should be defined in a lib folder to make it accessible in both places.

    On a side note, the reason for this is that in some cases you may want collections to be accessible on only the client, or only the server, but not both.