[Solved] Collection hooks in private meteor packages


I’m writing a private meteor package that will be used with an open-source meteor project in a commercial product. Is it possible to use collection hooks (matb33:collection-hooks) in my private package?

MY current issue is the collection objects are defined in the open-source project as global variables and are out-of-scope within my private package. If I try and re-define them in my private package, I get errors thrown by the DDP package saying that some methods are already defined, e.g.,

Error: A method named '/<collectionName>/insert' is already defined.

Are there any hacks to access collections within packages?


To use collection-hooks in your package:

In your package.js file add:

Package.onUse(function () {
  api.use("matb33:collection-hooks", ["client", "server"]);

More info:

Your collections should be visible in your package depending on if they were defined on client and server or both and if they were properly defined as global variables.


In the meteor project:

// collections/posts.js
Posts = new Meteor.Collection('posts');

In my package:

// package.js
Package.onUse(function(api) {
  api.use('matb33:collection-hooks@0.7.13', ['client', 'server']);

// collections/posts.js
Posts.after.insert = function (userId, doc) {

Console output:

ReferenceError: Posts is not defined
    at Router.map.route.path (packages/my:private-package/collections/posts.js:1:1)

I’ve defined my collection as a global variable in a folder (collection) accessible to both the client and the server. Is there anything else it might be?


The problem you are running into here is the fact that your package code executes prior to the application code. To fix this you can put any code that needs to run after the app loads inside of a Meteor.startup();

    Meteor.startup(function() {
        Posts.after.insert(function(userId, doc){


I don’t know how I forgot about execution order. That was it. Thank you!


It should be Posts = new **Mongo**.Collection('posts');