Meteor.subscribe issue

I had an issue with publications and subscriptions. I followed the project structure in the Meteor guide, I created a publication on my server side and I tried to subscribe to it in my template oncreated method but i was getting Meteor.subscribe is not a method. when I put the subscription in the code controlling my layout template, I don’t see my collection display any data. I finally got this to work by placing the Meteor.subscribe code in main.js file and it worked perfectly. I would like to get more explanation on why I wasn’t able to see my collection until I subscribed in the main.js file, is that file even a good place to subscribe to publications?

You should try to scope as much as possible to your templates, including subscriptions:

Template.myTemplate.onCreated(function() {
  this.autorun(() => {
    const mySub = this.subscribe('mySubscription');
    if (mySub.ready()) {
      console.log('subscription ready!');
    }
  });
});

If you scope everything to the template using this, then when the template is torn down, these things are cleaned up for you, and you don’t have to worry about things like stopping subscriptions, etc.

2 Likes

Thanks it now works, might have been due to a syntax error before