[Solved] Noob publish and subscribe

This is my first crack at meteor so bear with me. I’m trying to create a collection for my menu system, so in /server/bootstrap.js, I have

Meteor.startup(function () {
  var Pages = new Meteor.Collection('Pages');
  if(Pages.find().count() === 0) {
     Pages.insert( {'name': "Buildings", 'icon': "<i class='mdi-social-domain'></i>", 'route': 'buildings', 'include_in_menu': true, 'protected': false} );
     Pages.insert( {'name': "Map", 'icon': "<i class='mdi-maps-pin-drop'></i>", 'route': 'map', 'include_in_menu': true, 'protected': false} );
     Pages.insert( {'name': "Questions", 'icon': "Qu", 'route': 'questions', 'include_in_menu': true, 'protected': true} );
     Pages.insert( {'name': "Aggregations", 'icon': "Ag", 'route': 'aggregations', 'include_in_menu': true, 'protected': true} );
     Pages.insert( {'name': "Scenarios", 'icon': "Sc", 'route': 'scenarios', 'include_in_menu': true, 'protected': true} );
     Pages.insert( {'name': "Users", 'icon': "<i class='mdi-social-group'></i>", 'route': 'users', 'include_in_menu': true, 'protected': true} );

Then in /server/publish.js, I have:

Meteor.startup(function () {
  Meteor.publish("site_pages", function() {
     if(Roles.userIsInRole(this.userId, 'admin')) {
        return Pages.find();
     } else {
        return Pages.find({ 'protected': false });

Then in /my_app.js, I have:


At the console and in my code, ‘Pages’ is undefined. I know this is something simple, but it escapes me how I get access to my 6 Pages.

you declared Pages inside a function with the var statement, which means that variable is scoped to the function.

Make sure you declare Pages in a file that can be read on server and client and without the var

1 Like

I put

Pages = new Meteor.Collection(“Pages”);

at the top of /my_app.js (removing /server/bootstrap.js) and the application breaks with the following error

I20160118-12:07:37.690(-8)? Exception from sub directory id
Cursor or an array of Cursors
I20160118-12:07:37.691(-8)? at [object Object]..extend.
I20160118-12:07:37.691(-8)? at [object Object].
I20160118-12:07:37.691(-8)? at [object Object]..extend.
I20160118-12:07:37.692(-8)? at [object Object].
I20160118-12:07:37.692(-8)? at livedata_server.js:548:43

if you share all of your relevant code, maybe someone can help you. Meteor.subscribe should only run on the client

Oh wait… you are correct. Pages is now getting through and I can see the variable Pages.

I think I have some other parts of my code that are not quite working.
Thank you for your help.