Search code examples
meteorsimple-schema

Meteor pub/sub issues


This below is my collection code

Competitions = new Mongo.Collection("competitions");

var CompetitionsSchema = new SimpleSchema({
  year: {
      type: String
  },
  division: {
      type : String,
      allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
  },
  teams:{
      type : [TeamSchema],
      allowedValues: (function () {
         return Teams.find().fetch().map(function (doc) {
            return doc.name;
        });
      }()) //here we wrap the function as expression and invoke it
  }
}); 

In the allowedValues function

Teams.find is empty.

In the router I am subscribing to the publication as follows

 this.route('competitions', {
    path: '/admin/competitions',
    layoutTemplate: 'adminLayout',
    waitOn: function () {
        return [
            Meteor.subscribe('teams')
        ];
    }
});

This is my publish function

Meteor.publish('teams', function() {
  return  Teams.find({},{sort: {
    points: -1,
    netRunRate : -1
  }});
});

Do I have to do subscription some where else as well?


Solution

  • Your problem is in this piece of code:

      allowedValues: (function () {
         return Teams.find().fetch().map(function (doc) {
            return doc.name;
        });
      }()) //here we wrap the function as expression and invoke it
    

    This gets called when the page loads. At that point the Teams collection will still be empty on the client side. You need to wait until the data is ready. Since you are using waitOn in iron-router, it might be enough to just move this code to the onRendered callback.