Correct use of collections in a meteor package?
I am trying to create a package that uses a collection. In a normal application, the same code works fine without any problem.
collectiontest.js (package code)
// Why do I need to set the global property manually?
var globals = this || window;
TestCol = new Meteor.Collection('test');
globals.TestCol = TestCol;
console.log('defined');
if(Meteor.isServer){
Meteor.publish('test', function(){
return TestCol.find();
});
Meteor.startup(function(){
console.log('wtf');
TestCol.remove({});
TestCol.insert({test: 'a document'});
});
};
if(Meteor.isClient){
Meteor.subscribe('test');
};
The test goes through the client and server:
Tinytest.add('example', function (test) {
console.log('here');
var doc = TestCol.findOne();
test.equal(doc.test, 'a document');
console.log(doc);
});
But if I open the developer tools on the client and run:
TestCol.find().count()
The result is 0. Why? Also, why do I need to have a string globals.TestCol = TestCol;
to run tests at all? Without this line, the error: TestCol is not defined on both the server and the client.
source to share
The objects defined in the package can be referenced in your application after use api.export
.
In your case:
api.export('TestCol',['server','client']);
Above the line will appear TestCol
as a global variable and it will be accessible from your application.
source to share