I have this code where I am finding a monitor in the database with these queries
const monitorfind = await monitorschema.findOne({ Price: { $lte: Number(reqbody.price) } }, {Size: {$gte: Number(reqbody.size - 8)}}, {hz: {$gte: Number(reqbody.hz - 25)}}, {Resolution: {$gte: Number(reqbody.res)}}, {ResponseTime: {$lte: Number(reqbody.ms 4)}})
When I run the code, I get this error:
throw new MongooseError('Callback must be a function, got ' callback);
^
MongooseError: Callback must be a function, got [object Object]
at Function.Model.$handleCallbackError (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\mongoose\lib\model.js:4924:11)
at Function.findOne (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\mongoose\lib\model.js:2248:19)
at C:\Users\Sochum\Desktop\pickitly_web_main\routes\monitors.js:23:49
at Layer.handle [as handle_request] (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:275:10)
How do I fix this?
CodePudding user response:
I think your reqbody
is wrong pattern.right pattern is req.body
.but you use any middleware function
for passing data at time use code otherwise you can use req.body
for your code.
try this
const conditions = {
Price: { $lte: Number(reqbody.price) },
Size: {$gte: Number(reqbody.size - 8)},
hz: {$gte: Number(reqbody.hz - 25)},
Resolution: {$gte: Number(reqbody.res)},
ResponseTime: {$lte: Number(reqbody.ms 4)}
}
monitorschema.findOne(conditions, function (err, monitor) {
console.log(monitor) //If matching any data it is show here
});
CodePudding user response:
You need to chain exec()
function after findOne
. It is needed if we want to use async-await
. Changing query to following should work:
const monitorfind = await monitorschema.findOne({
Price: { $lte: Number(reqbody.price) },
Size: {$gte: Number(reqbody.size - 8)},
hz: {$gte: Number(reqbody.hz - 25)},
Resolution: {$gte: Number(reqbody.res)},
ResponseTime: {$lte: Number(reqbody.ms 4)}
}).exec()
Reference: https://masteringjs.io/tutorials/mongoose/promise
In case you are using older version of mongoose (<5), you will have to pass callback function as second param to findOne
as suggested in first answer.