{"record":{"id":"d10e7b3805860aea","repo":"Automattic/mongoose","slug":"model-findbyid-no-longer-accepts-a-callback","errorCode":null,"errorMessage":"Model.findById() no longer accepts a callback","messagePattern":"Model\\.findById\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":2166,"sourceCode":" *     await Adventure.findById(id).exec();\n *\n *     // select only the adventures name and length\n *     await Adventure.findById(id, 'name length').exec();\n *\n * @param {any} id value of `_id` to query by\n * @param {object|string|string[]} [projection] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.select())\n * @param {object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions())\n * @return {Query}\n * @see field selection https://mongoosejs.com/docs/api/query.html#Query.prototype.select()\n * @see lean queries https://mongoosejs.com/docs/tutorials/lean.html\n * @see findById in Mongoose https://masteringjs.io/tutorials/mongoose/find-by-id\n * @api public\n */\n\nModel.findById = function findById(id, projection, options) {\n  _checkContext(this, 'findById');\n  if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function' || typeof arguments[2] === 'function') {\n    throw new MongooseError('Model.findById() no longer accepts a callback');\n  }\n\n  return this.findOne({ _id: id }, projection, options);\n};\n\n/**\n * Finds one document.\n *\n * The `conditions` are cast to their respective SchemaTypes before the command is sent.\n *\n * *Note:* `conditions` is optional, and if `conditions` is null or undefined,\n * mongoose will send an empty `findOne` command to MongoDB, which will return\n * an arbitrary document. If you're querying by `_id`, use `findById()` instead.\n *\n * #### Example:\n *\n *     // Find one adventure whose `country` is 'Croatia', otherwise `null`\n *     await Adventure.findOne({ country: 'Croatia' }).exec();","sourceCodeStart":2148,"sourceCodeEnd":2184,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L2148-L2184","documentation":"Model.findById(id, projection, options) is sugar for findOne({ _id: id }); Mongoose 7 removed callbacks, so passing a function in any of the first three argument slots throws before the findOne delegation happens. Note a callback passed as `id` itself also triggers this.","triggerScenarios":"`User.findById(id, (err, user) => ...)`; `User.findById(cb)`; older route handlers from Express + Mongoose 5/6 tutorials.","commonSituations":"Classic `GET /users/:id` handlers copied from pre-2023 tutorials; the most-hit call site family after find() during major upgrades.","solutions":["Await it: `const user = await User.findById(id);`","Handle not-found explicitly (result is null when missing)","Migrate route handlers in bulk — search `findById\\(` with a following function argument"],"exampleFix":"// before\nUser.findById(req.params.id, (err, user) => {\n  if (err) return next(err);\n  res.json(user);\n});\n\n// after\nconst user = await User.findById(req.params.id);\nres.json(user);","handlingStrategy":"validation","validationCode":"const isFn = (a) => typeof a === 'function';\nif ([id, projection, options].some(isFn)) {\n  throw new TypeError('findById() is promise-only');\n}\nconst user = await User.findById(id, projection, options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Handle the null result explicitly (404 path) — the old callback's err argument never covered missing docs","TypeScript types remove the callback overloads; adopt them at the API boundary"],"tags":["mongoose","callback-removed","promise","query","migration"],"backgroundTag":"mongoose-callback-removed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}