[Solved] findOne error detected

Hello everybody,
I have a problem with error recovery during ‘findOne’ on a Mongo basis.
This works

	let lm =collection.findOne({_id:idDoc})
	console.log(lm)

This does not work

	let lm =collection.findOne({_id:idDoc},function(err,result){
		if(err){}
		if(result){}
	});
	console.log(lm)

next runtime error

Exception from Tracker afterFlush function:
Error: Match error: Failed Match.OneOf, Match.Maybe or Match.Optional validation
at check (match.js:36)
at Collection._getFindOptions (collection.js:295)
at Collection.findOne (collection.js:358)
at collectionTest (dbFunctions.js:31)
at Blaze.TemplateInstance.Template.BQESAIECR.rendered (lesbanquesSaiEcr.js:12)
at template.js:119
at Function.Template._withTemplateInstanceFunc (template.js:490)
at fireCallbacks (template.js:115)
at Blaze.View. (template.js:208)
at view.js:107

I can’t find where this error comes from.
can someone tell me which direction i should look to solve this problem.
Thank you for your answers.
YC

findOne does not have a callback option - doc. If you want to use a callback, you will need to do something like:

collection.find({_id:idDoc}, (err, result) => {
  if (err) {
    // handle error
  } else {
    let lm = result.fetch()[0];
    console.log(lm);
  }
});

However, you need to ensure any code requiring lm is run inside the callback. In Meteor, you could set a ReactiveVar within the callback and make use of reactivity to get the result outside the callback.

Thank you for your response, and excuse me, maybe I could have read the documentation with a little more attention !!

1 Like