Meteor code must always run within a Fiber when doing Collection.findOne

Hi all,
I’m getting this error when doing a findOne on some mongo collection in a server method.
All these code run fine before. I just upgraded meteor from 1.6.0.1 to 1.10.2.

This is the code that is producing the error:

let moneda = {};
let compania = {};
let suscriptor = {};
let asegurado = null;

result.forEach(cuota => {

	// aparently, this findOne produces the error
    moneda = Monedas.findOne(cuota.moneda, { fields: { simbolo: 1, descripcion: 1, }});
	compania = Companias.findOne(cuota.compania, { fields: { abreviatura : 1, nombre: 1, }});
	suscriptor = Suscriptores.findOne(cuota.suscriptor, { fields: { abreviatura : 1 }});
...  ... 

After the upgrade, everything seemed to work fine, until I checked a little more and found errors on server code that do a findOne on a mongodb collection. From what I can see, although meteor upgraded several packages when upgrading from 1.6 to 1.10, matb33:collection-hooks was not upgraded. It was in version 0.8.1 and is still there.

I mention this because this package is shown in the error message:

Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
at Object.Meteor._nodeCodeMustBeInFiber (packages\meteor.js:1186:11)
at Meteor.EnvironmentVariable.EVp.get (packages\meteor.js:1199:10)
at Object.collection. [as findOne] (packages\matb33_collection-hooks.js:129:37)
at ns.Collection.findOne (packages/mongo/collection.js:356:29)
at server/methods/consultas/consultaCuotasPendientesCobroVencimientos/consultas_MontosPendientesCobro_Vencimientos.js:211:30

Of course I’m not using non meteor code or something similar here. Just normal meteor code and, as I said before, code that run fine for years before the upgrade.

Can somebody shed some light into this and help me solve this issue?
Many thanks and bye …

This looks like an error caused in one of your files, or in a package which uses matb33:collection-hooks. It has before.findOne and after.findOne hooks (https://github.com/Meteor-Community-Packages/meteor-collection-hooks#beforefindoneuserid-selector-options), so likely some piece of code in one of them is failing.

Personally I don’t think is matb33:collection-hooks itself that fails, or that the latest Meteor update has an issue. What’s more likely is that some defective code somewhere has just been revealed. Could be simply a setTimeout, or setInterval, likely in one of the hooks above.

Have you tried to bind the foreach callback?

result.forEach(Meteor.bindEnvironment(cuota => {}));

Hi there and many thanks for your answers.

After doing some testing I think I’ve found the issue. The problem is with package: meteorhacks:aggregate@=1.3.0. After removing it from the affected code and doing some refactoring everything started working again. It was easy to leave out the use of the package because I used it in a very light way. However, I don’t know why it was matb33:collection-hooks which was complaining in the error message.

I think I use the aggregate package in another code, but it runs fine there. I haven’t yet tested everything thoroughly anyway. As I said, these code has many years running fine and I didn’t modify it this time. I think it has to do with the change of meteor version, but I’m not completely sure.

So thanks for your help and bye