3
我想創建一個使用集合的包。在正常的應用程序中,相同的代碼工作正常,沒有任何問題。在流星包中正確使用集合?
collectiontest.js(封裝代碼)
// 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');
};
測試通過在客戶端和服務器:
Tinytest.add('example', function (test) {
console.log('here');
var doc = TestCol.findOne();
test.equal(doc.test, 'a document');
console.log(doc);
});
但是,如果我打開客戶端開發工具和運行:
TestCol.find().count()
結果爲0。爲什麼? 另外,爲什麼我必須有行globals.TestCol = TestCol;
的測試運行呢?如果沒有該行,則會出現錯誤:TestCol未在服務器和客戶端上定義。