mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

MongooseError: Query.prototype.countDocuments() no longer accepts a callback

Open mavviiss opened this issue 1 year ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.0.3

Node.js version

18.15.0

MongoDB server version

6.0.5

Typescript version (if applicable)

No response

Description

Error: Service validation failed: serviceName: Query.prototype.countDocuments() no longer accepts a callback, _id: Query.prototype.countDocuments() no longer accepts a callback at ValidationError.inspect (/home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/error/validation.js:50:26) at formatValue (node:internal/util/inspect:806:19) at inspect (node:internal/util/inspect:365:10) at formatWithOptionsInternal (node:internal/util/inspect:2273:40) at formatWithOptions (node:internal/util/inspect:2135:10) at console.value (node:internal/console/constructor:340:14) at console.log (node:internal/console/constructor:377:61) at _default (/home/mavis/projects/infrastructureServerNode/source/middlewares/error.middleware.js:5:13) at Layer.handle_error (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/layer.js:71:5) at trim_prefix (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:326:13) at /home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:286:9 at Function.process_params (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:346:12) at next (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:280:10) at Layer.handle_error (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/layer.js:67:12) at trim_prefix (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:326:13) at /home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:286:9 at Function.process_params (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:346:12) at next (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:280:10) at Layer.handle_error (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/layer.js:67:12) at trim_prefix (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:326:13) at /home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:286:9 at Function.process_params (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:346:12) at Immediate.next [as _onImmediate] (/home/mavis/projects/infrastructureServerNode/node_modules/express/lib/router/index.js:280:10)

Steps to Reproduce

ValidatorError: Query.prototype.countDocuments() no longer accepts a callback
       at validate (/home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/schematype.js:1347:13)
       at /home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/schematype.js:1328:11
       at processTicksAndRejections (node:internal/process/task_queues:95:5) {
     properties: [Object],
     kind: 'unique',
     path: 'serviceName',
     value: 'VPS[AAA]-1680608641830',
     reason: MongooseError: Query.prototype.countDocuments() no longer accepts a callback
         at model.Query.Query.countDocuments (/home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/query.js:2752:11)
         at /home/mavis/projects/infrastructureServerNode/node_modules/mongoose-unique-validator/index.js:123:52
         at new Promise (<anonymous>)
         at model.<anonymous> (/home/mavis/projects/infrastructureServerNode/node_modules/mongoose-unique-validator/index.js:54:32)
         at SchemaString.SchemaType.doValidate (/home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/schematype.js:1311:24)
         at /home/mavis/projects/infrastructureServerNode/node_modules/mongoose/lib/document.js:2872:18
         at processTicksAndRejections (node:internal/process/task_queues:77:11),
     [Symbol(mongoose:validatorError)]: true
   }

Expected Behavior

No response

mavviiss avatar Apr 04 '23 11:04 mavviiss

Based on your stack trace, it looks like this error is coming from mongoose-unique-validator. Please follow this PR for updates: https://github.com/mongoose-unique-validator/mongoose-unique-validator/pull/143

vkarpov15 avatar Apr 05 '23 17:04 vkarpov15

https://stackoverflow.com/questions/75586474/mongoose-stopped-accepting-callbacks-for-some-of-its-functions

Apparently countDocuments() doesn't accept call backs anymore so you should use await/async instead.

donoftime2018 avatar Oct 25 '23 00:10 donoftime2018