Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw new MongooseError('Query.prototype.exec() no longer accepts a callback');

Tags:

mongoose

throw new MongooseError('Query.prototype.exec() no longer accepts a callback');
          ^
 
MongooseError: Query.prototype.exec() no longer accepts a callback

I was trying to register users.

like image 495
Francisco Salazar Avatar asked Feb 15 '26 21:02

Francisco Salazar


1 Answers

I had this same issue and fixed it with two solutions:

  1. Downgrade your Mongoose. The latest versions stopped accepting callbacks. I personally downgraded to v6.10.0
npm uninstall mongoose
npm install [email protected]
  1. Change the callback function to a then/catch methods or use async/await. So for example, instead of:
myModel.find({}).exec((err, res) => {
  //callback code
});

Just remove your .exec() function and replace it with .then()/.catch():

myModel.find({}).then((res) => {
  //if succeded do this block of code
}).catch((err) => {
  //catch error
});
like image 115
Gabriel Santos Rezende Avatar answered Feb 21 '26 16:02

Gabriel Santos Rezende



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!