Meteor:在服务器崩溃的Tracker.autorun中找到了一个......怎么办?

时间:2015-12-09 16:46:51

标签: meteor meteor-tracker

我想从存储在数据库中的列表中填充受限单词的内存列表,因为希望它是动态的。声音简单吧?好吧不,它不是!为什么?

以下是它的简短代码:

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允许您运行依赖于被动的功能   数据源。每当使用新数据更新这些数据源时,   该功能将重新运行。

然而,这种方法会使用我无法理解的堆栈跟踪崩溃大时间

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)

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

1 个答案:

答案 0 :(得分:2)

您不能在服务器上使用自动运行;它只是客户端功能。

在“find”返回的光标上使用cursor.observe或cursor.observeChanges:

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

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

如果你的集合中有多个项目,我也会避免使用findOne(不确定你是否只是在这里省略了这些参数,或者只是将它用作测试用例)。