subscriptionHandle.stop() is not a function when self.onStop() is called in publish

Hey everybody at the awesome meteor community!

One thing I like about these forums vs stackoverflow is you can say things like that.

Anyways to my question:
I made a publish function that uses self.added, etc. so I can modify documents before publishing them. The publishing part works great. However, whenever I leave the page on the client so that the subscription stops, I get this error Exception in defer callback: TypeError: subscriptionHandle.stop is not a function.

The meteor docs show that you are supposed to stop the handle when the pubish function’s onStop is called, but it’s not working for me. I would just say if(typeof subscriptionHandle === 'function'){ subscriptionHandle.stop() }, but I’m afraid that in doing that I may end up with a massive memory leak. Here’s my publish code. Anybody have an idea what’s going on?

var subscriptionHandle = Items.find(searchCriteria, safeOptions);
subscriptionHandle.observeChanges({
	added: function(_id, doc) {
		console.log("added ");
		doc.isInItemsList = true
		self.added(collectionName, _id, doc);
	},
	changed: function(id, fields) {
		console.log("changed")
		self.changed(collectionName, id, fields);
	},
	removed: function(id) {
		console.log("removed")
		self.removed(collectionName, id);
	}
});

self.ready();

self.onStop(function() {
	console.log('stopping', subscriptionHandle)
	subscriptionHandle.stop();
});

I figured it out…

I was calling stop() on the cursor, not on the actual subscription handle. To fix, I just changed the first to lines to this:

var cursor = Items.find(searchCriteria, safeOptions);
var subscriptionHandle = cursor.observeChanges({

Maybe this can help someone down the road :slight_smile: