meteormeteor-packages

How to get Meteor.users collection on a Meteor package?


I'm developing a test frameword where I want to reset all collections on each run.

I'm having problems doing this with Meteor.users, since it's undefined on the package environment.

Is there any workaround to clean the Meteor.users collection from within a package?


Solution

  • The package load order and exported variables matter when it comes to symbol availability in packages.

    In order to get a symbol created in a package, it has to be loaded prior to your package. This can be achieved by depending on it or on some other package that depends on it.

    Examples of such symbols are methods added to the Meteor object, and this is what is happening in your case.

    api.use('some-package'); // some-package is the desired package or depends on it
    

    In order to get a symbol that is exported by a package, you should either directly depend on it, or depend on a package that implies this package (or explicitly exports the symbol on its own as well).

    api.use('some-package'); // some-package exports the symbol directly or by implying
    

    You can make this dependency weak if you don't want the package to be added to the bundle if your package is the only one that depends on it (i.e, there are no other packages that strongly depend on it, and the user did not add it as a top-level dependency).

    api.use(['some-package'], ['client', 'server'], {weak: true});
    

    If you need to do something after all of the packages have loaded, you can do so with Meteor.startup():

    Meteor.startup(function() {
      //do stuff after all packages and code were loaded
    });