【问题标题】:Meteor: collection.find().fetch() inside Deps.autorun failsMeteor:Deps.autorun 中的 collection.find().fetch() 失败
【发布时间】:2015-04-20 22:23:26
【问题描述】:

我想在流星服务器上创建一个反应式集合查询,并在有变化时处理结果。

这是我的代码:

if (Meteor.isServer) {
  Meteor.startup(function(){
    Deps.autorun(function(){
      var items=new Meteor.Collection('name').find().fetch();
      // ... process the items ...
    });
  });
}

(事实上,对于这个测试,整个项目只包含一个 .js 文件中的上述代码)。 使用meteor 启动此程序会引发错误:

/home/yaakov/Bug/.meteor/local/build/programs/server/boot.js:198
}).run();
   ^
Error: Can't call yield in a noYieldsAllowed block!
    at Function.Fiber.yield (packages/meteor/fiber_helpers.js:11)
    at Function.wait (home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:111:14)
    at Object.Future.wait (/home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:325:10)
    at new MongoConnection (packages/mongo-livedata/mongo_driver.js:196)
    at new MongoInternals.RemoteCollectionDriver (packages/mongo-livedata/remote_collection_driver.js:4)
    at Object.<anonymous> (packages/mongo-livedata/remote_collection_driver.js:44)
    at Object.defaultRemoteCollectionDriver (packages/underscore/underscore.js:750)
    at new Meteor.Collection (packages/mongo-livedata/collection.js:72)
    at app/Bug.js:4:17
    at packages/deps/deps.js:47

我做错了什么?如何在流星服务器上创建响应式集合查询?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    Deps 包只能在客户端工作,所以你不能在服务器上使用Deps.autorun

    要在服务器上使用响应式查询,请改用observe

    var items=collection.find().observe({
        added: function (document) {
            // ...
        },
        changed: function (newDocument, oldDocument) {
            // ...
        },
        removed: function (oldDocument) {
            // ...
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-20
      • 2015-07-15
      • 2017-12-02
      • 2014-02-17
      • 2021-12-29
      • 2015-07-06
      • 2019-12-18
      • 1970-01-01
      相关资源
      最近更新 更多