{"record":{"id":"9f73ae1d42a054d2","repo":"Automattic/mongoose","slug":"model-ensureindexes-no-longer-accepts-a-callback","errorCode":null,"errorMessage":"Model.ensureIndexes() no longer accepts a callback","messagePattern":"Model\\.ensureIndexes\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":1660,"sourceCode":" *\n *     const eventSchema = new Schema({ thing: { type: 'string', unique: true } })\n *     const Event = mongoose.model('Event', eventSchema);\n *\n *     Event.on('index', function (err) {\n *       if (err) console.error(err); // error occurred during index creation\n *     });\n *\n * _NOTE: It is not recommended that you run this in production. Index creation may impact database performance depending on your load. Use with caution._\n *\n * @param {object} [options] internal options\n * @return {Promise}\n * @api public\n */\n\nModel.ensureIndexes = async function ensureIndexes(options) {\n  _checkContext(this, 'ensureIndexes');\n  if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {\n    throw new MongooseError('Model.ensureIndexes() no longer accepts a callback');\n  }\n\n  await new Promise((resolve, reject) => {\n    _ensureIndexes(this, options, (err) => {\n      if (err != null) {\n        return reject(err);\n      }\n      resolve();\n    });\n  });\n};\n\n/**\n * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/7.0/classes/Db.html#createIndex)\n * function.\n *\n * @param {object} [options] internal options\n * @return {Promise}","sourceCodeStart":1642,"sourceCodeEnd":1678,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L1642-L1678","documentation":"Model.ensureIndexes() builds all indexes declared in the schema (skipping already-existing ones) and is async-only in Mongoose 7+; a function argument throws right after the context check. Prefer letting `autoIndex`/`Model.init()` handle this and avoid manual calls in production.","triggerScenarios":"`User.ensureIndexes(cb)`; `User.ensureIndexes({}, cb)`; startup code following older guides that wire index builds with callbacks.","commonSituations":"Mongoose major upgrades; codebases where index creation lived in a bootstrap file with callback-based sequencing.","solutions":["Await it: `await User.ensureIndexes();`","Better: rely on connection autoIndex (`await mongoose.connect(uri, { autoIndex: true })`) and drop manual calls","Grep for `ensureIndexes\\(` with function arguments during the migration pass"],"exampleFix":"// before\nUser.ensureIndexes(function(err) { if (err) throw err; });\n\n// after\nawait User.ensureIndexes();","handlingStrategy":"validation","validationCode":"const isFn = (a) => typeof a === 'function';\nif (isFn(options)) throw new TypeError('ensureIndexes() is promise-only');\nawait User.ensureIndexes(options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid manual ensureIndexes in production — set autoIndex appropriately and let connect/init handle it","During upgrade, delete callback plumbing rather than adapting it"],"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"}