流星:服务器崩溃 Tracker.autorun 中的 findOne ... 怎么办?

Meteor : findOne in Tracker.autorun on server crashes ... what to do?

我想从存储到数据库中的列表中填充内存中的限制词列表,因为需要是动态的。听起来很简单吧?不,不是!为什么 ?

这是它的一个简短代码:

if (Meteor.isServer) {
  Meteor.startup(function() {

    var configCollection = new Mongo.Collection('config');

    // An wrapper object for easy referencing
    var words = {
      faulty: ['f_word_here']
    }; // Some default faulty words

    var updateFaultyWords = function() {
      var config = configCollection.findOne();
      if (config) {
        words.faulty = config.faultyWords;
      }
    };

    // ------- Problematic Code ------

    Tracker.autorun(function() {
      updateFaultyWords();
    });


    // -------------------------------

    // later somewhere in the code

    var allowWord = function(word) {
      return words.faulty.indexOf(word) === -1;
    };
  });
}

我在这里使用 Tracker,因为就像它在文档中所说的那样,我想以反应模式更新我的列表。

Tracker.autorun allows you to run a function that depends on reactive data sources. Whenever those data sources are updated with new data, the function will be rerun.

然而,此方法崩溃很长时间,堆栈跟踪我无法理解:

W20151209-17:36:55.802(1)? (STDERR)           
W20151209-17:36:55.802(1)? (STDERR) /Users/tiberiu/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
W20151209-17:36:55.802(1)? (STDERR)                         throw(ex);
W20151209-17:36:55.803(1)? (STDERR)                               ^
W20151209-17:36:55.865(1)? (STDERR) Error: Can't call yield in a noYieldsAllowed block!
W20151209-17:36:55.865(1)? (STDERR)     at Function.Fiber.yield (packages/meteor/fiber_helpers.js:8:1)
W20151209-17:36:55.865(1)? (STDERR)     at Function.wait (/Users/tiberiu/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:183:14)
W20151209-17:36:55.865(1)? (STDERR)     at Object.Future.wait (/Users/tiberiu/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:397:10)
W20151209-17:36:55.865(1)? (STDERR)     at [object Object]._.extend._nextObject (packages/mongo/mongo_driver.js:986:1)
W20151209-17:36:55.865(1)? (STDERR)     at [object Object]._.extend.forEach (packages/mongo/mongo_driver.js:1020:1)
W20151209-17:36:55.866(1)? (STDERR)     at [object Object]._.extend.map (packages/mongo/mongo_driver.js:1030:1)
W20151209-17:36:55.866(1)? (STDERR)     at [object Object]._.extend.fetch (packages/mongo/mongo_driver.js:1054:1)
W20151209-17:36:55.866(1)? (STDERR)     at [object Object].Cursor.(anonymous function) [as fetch] (packages/mongo/mongo_driver.js:869:1)
W20151209-17:36:55.866(1)? (STDERR)     at [object Object].MongoConnection.findOne (packages/mongo/mongo_driver.js:776:1)
W20151209-17:36:55.867(1)? (STDERR)     at [object Object]._.extend.findOne (packages/mongo/collection.js:305:1)

我做错了什么?我应该将此报告为错误吗?

您不能在服务器上使用自动运行;它是 client-side 唯一的功能。

使用 cursor.observe 或 cursor.observe 对 "find" 返回的游标进行更改:

http://docs.meteor.com/#/full/observe

http://docs.meteor.com/#/full/observe_changes

如果您的 collection 中有多个项目,我也会避免使用 findOne(不确定您是只是省略此处的参数还是只是将其用作测试用例)。