{"record":{"id":"0f1c58e4faeb8fd7","repo":"Automattic/mongoose","slug":"model-listindexes-no-longer-accepts-a-callback","errorCode":null,"errorMessage":"Model.listIndexes() no longer accepts a callback","messagePattern":"Model\\.listIndexes\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":1619,"sourceCode":"  }\n\n  return toDrop;\n}\n\n/**\n * Lists the indexes currently defined in MongoDB. This may or may not be\n * the same as the indexes defined in your schema depending on whether you\n * use the [`autoIndex` option](https://mongoosejs.com/docs/guide.html#autoIndex) and if you\n * build indexes manually.\n *\n * @return {Promise}\n * @api public\n */\n\nModel.listIndexes = async function listIndexes() {\n  _checkContext(this, 'listIndexes');\n  if (typeof arguments[0] === 'function') {\n    throw new MongooseError('Model.listIndexes() no longer accepts a callback');\n  }\n\n  if (this.$__collection.buffer) {\n    await new Promise(resolve => {\n      this.$__collection.addQueue(resolve);\n    });\n  }\n\n  return this.$__collection.listIndexes().toArray();\n};\n\n/**\n * Sends `createIndex` commands to mongo for each index declared in the schema.\n * The `createIndex` commands are sent in series.\n *\n * #### Example:\n *\n *     await Event.ensureIndexes();","sourceCodeStart":1601,"sourceCodeEnd":1637,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L1601-L1637","documentation":"Model.listIndexes() returns the indexes currently defined in MongoDB (which can differ from schema indexes depending on autoIndex usage). It takes no arguments in Mongoose 7+; passing a callback function throws immediately — it never reaches the driver.","triggerScenarios":"`User.listIndexes(cb)`; `User.listIndexes().toArray(cb)` confusion (the promise already resolves to an array-like); diagnostic scripts from the callback era.","commonSituations":"Post-migration leftovers; monitoring/diagnostic endpoints written for Mongoose 6.","solutions":["Await it: `const indexes = await User.listIndexes();`","The result is an array-like from the driver cursor; iterate with `for...of` or spread `[...]`","Compare against `User.schema.indexes()` for drift detection instead of hand-rolled callback diffing"],"exampleFix":"// before\nUser.listIndexes((err, indexes) => { ... });\n\n// after\nconst indexes = await User.listIndexes();","handlingStrategy":"validation","validationCode":"if (typeof arguments[0] === 'function') throw new TypeError('listIndexes() takes no arguments');\nconst indexes = await User.listIndexes();","typeGuard":null,"tryCatchPattern":"try {\n  const indexes = await User.listIndexes();\n} catch (err) {\n  if (err?.codeName === 'NamespaceNotFound') return []; // collection not created yet\n  throw err;\n}","preventionTips":["Remember listIndexes() rejects with NamespaceNotFound before the collection exists — handle that, not callbacks","Use schema.indexes() vs listIndexes() diffs for drift reports in promise-based tooling"],"tags":["mongoose","callback-removed","promise","indexes","migration"],"backgroundTag":"mongoose-callback-removed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}