【发布时间】:2014-11-17 03:26:53
【问题描述】:
我正在尝试创建一个使用集合的包。在正常的应用程序中,相同的代码可以正常工作,没有任何问题。
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 is not defined。
【问题讨论】:
标签: collections meteor package