I want to populate an in-memory list of restricted words form a list stored into the database, because is desired to be dynamic. Sound simple huh? Well no, it is not! Why ?
Here is a short code of it :
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;
};
});
}
I am using Tracker here because like it says in the docs I want to update my list in a reactive mode.
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.
However this method crashes big time with a stack trace that I cannot understand:
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)
What am I doing wrong ? Should I report this as a bug ?
You cannot use autorun on the server; it is client-side only functionality.
Use cursor.observe or cursor.observeChanges on the cursor that is returned by "find":
http://docs.meteor.com/#/full/observe
http://docs.meteor.com/#/full/observe_changes
I would also avoid using findOne if you have more than item in your collection (not sure if you're just omitting the arguments here or are just using it as a test case).