Problem with template subscription

Hey guys,

I’m running into a weird problem with my template subscriptions. This is my code:

Template.editAppointment.onCreated(function() {
   var self = this;
   self.autorun(function() {
      self.subscribe("appointment", Router.current().params._id);
   });
});

Pretty straightforward, I guess. The problem is that I’m getting this console error whenever I reach this template:

Uncaught Error: Can't call View#subscribe from inside the destroyed callback, try calling it inside created or rendered.

As you can see, it is inside a created event. For the records, I’m using Iron Router.

What could I be missing here?

I guess the self.autorun fires because Router.current() is reactive, but when you change the route, the template gets destroyed and the autorun is still active.

My guess is that Iron Router pre-dates template based subscriptions and thus you run into uncharted territory, maybe?

1 Like

Right in the spot sir. Nothing like a fresh pair of eyes :smile:

Template.editAppointment.onCreated(function() {
   var self = this;
   var id = Router.current().params._id;
   self.autorun(function() {
     self.subscribe("appointment", id);
   });
});

Doing this did the trick. Thanks!