【问题标题】:Correctly using collections in a meteor package?正确使用流星包中的集合?
【发布时间】: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


    【解决方案1】:

    一旦您使用api.export,就可以在您的应用程序中引用包中定义的对象。

    在你的情况下:

    api.export('TestCol',['server','client']);
    

    上面的行会将TestCol 公开为全局变量,并且可以从您的应用程序中访问。

    【讨论】:

      猜你喜欢
      • 2013-08-24
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多