"Meteor code must always run within a Fiber" error

Here’s the deal - I use a 3-rd party npm module that reads some config data from Mongo DB. I override the function that is used in many places inside that module:

mymodule.config() = function(){
    return {
         get: function(key, otherwise) {
             // HERE I NEED TO GET DATA FROM COLLECTION
             return ConfigCollection.find({key: key}).findOne();
         }
    }
}

Of course, this gives “Meteor code must always run within a Fiber” error. However I tried wrapping the get function contents around Meteor.wrapAsync function and call it, but is throws the same error. Also tried some Meteor.bindEnvironment kung-fu, but there is no callback to wrap it around. How could this be achieved? What I need is to override mymodule.config().get(...) function so it reads some config from collection and return it. I can’t (and I don’t want) touch anything in the 3-rd party module. Any ideas are more than welcome!

Is this what you tried?

getConfig = Meteor.bindEnvironment(function (key) {
  return ConfigCollection.findOne({key: key});
});

mymodule.config() = function(){
    return {
         get: function(key, otherwise) {
             return getConfig(key);
         }
    }
}

Disclaimer; no idea if that will work in this case :stuck_out_tongue:

2 Likes

Worked like a charm!
Disclaimer; no idea why I didn’t tried it that way! :smile:

1 Like

I was too fast on happiness.
The problem is that get() always returns undefined. Seems it doesn’t wait the function to complete.

getConfig = Meteor.bindEnvironment(function (key) {
   var val = ConfigCollection.findOne({key: key});
   console.log('1', val);
   return val;
});

mymodule.config() = function(){
    return {
         get: function(key, otherwise) {
             var val = getConfig(key);
             console.log('2', val);
             return val;
         }
    }
}

Console logs:

2 undefined
1 {_id:..., data:{}}

I’ve tried to use Future in order to return the value, but I’ve got “Can’t wait without a fiber” error. Isn’t the idea of Meteor.bindEnvironment to exactly create and run code in a fiber? I really can’t understand what is going on here. :frowning:

I’m facing a solid wall on this one and I’m unable to workaround it. Any ideas or hints are more than welcome.

Yeah the issue is that your Meteor.bindEnvironment function performs asynchronously but your get function returns synchronously. Without knowing exactly what you’re trying to do with the package, I’m not sure whether you can use the Collections for this.

Hi and thanks for the reply. Really appreciate it. What I try to do is to overwrite mymodule.config().get(...) method in order to get config data stored in collection. In original this method retrieves config data from a JSON file. The thing is it expects to return data synchronously and reading from collection is asynchronously thing. But isn’t that the reason behind Meteor.bindEnvironment and Meteor.wrapAsync? I thought they create a Fiber if there isn’t and run the code in fiber.