【问题标题】:Meteor collection findOne returning undefined when called in callback流星集合 findOne 在回调中调用时返回未定义
【发布时间】:2015-10-17 22:46:44
【问题描述】:

我正在尝试在 Meteor 集合中查找 ().fetch(),但是当我在回调中调用此代码时(或直接在控制台中,它返回 [])。

MyClass = class MyClass {
    constructor() {
        this.worker = null;
        this._init();

        console.log(Site.MyCollection.find().fetch()); //Logs the array of documents
    }

    _init() {
        this.worker = new Worker('my-web-worker.js');
        this.worker.addEventListener('message', this._onMessage, false);

        this.worker.postMessage({ cmd: 'start', data: Site.MyCollection.find().fetch() }); //Works fine, the Web Worker receives the array of documents as expected

    }


    _onMessage(e) {
        console.log(Site.MyCollection.find().fetch()); //Logs []
    }
}

我无法从集合中删除任何文档,因此我不确定这是否是一项安全功能,或者集合是否在创建后被擦除。

为了创建(虚拟)集合,我正在这样做(在客户端和服务器上)

Site.MyCollection = new Mongo.Collection("my-collection");

Site.MyCollection.remove();

let docs = [
  {name:"One", data : ["a", "b", "c"}},
  {name:"Two", data : ["e", "f", "g"}},
  {name:"Three", data : ["h", "i", "j"}},
  {name:"Four", data : ["k", "l", "m"}}   
];

_.each(docs, function(doc){
  Site.MyCollection.insert(doc);
});

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    啊,原来我是个白痴,我已经删除了自动发布包。所以

    $meteor add autopublish
    

    然后重新启动应用程序修复它。

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      相关资源
      最近更新 更多