{"record":{"id":"374c5aecce0acd42","repo":"Automattic/mongoose","slug":"model-createindexes-no-longer-accepts-a-callback","errorCode":null,"errorMessage":"Model.createIndexes() no longer accepts a callback","messagePattern":"Model\\.createIndexes\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":1686,"sourceCode":"      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}\n * @api public\n */\n\nModel.createIndexes = async function createIndexes(options) {\n  _checkContext(this, 'createIndexes');\n\n  if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {\n    throw new MongooseError('Model.createIndexes() no longer accepts a callback');\n  }\n\n  return this.ensureIndexes(options);\n};\n\n\n/*!\n * ignore\n */\n\nfunction _ensureIndexes(model, options, callback) {\n  const indexes = Array.isArray(options?.toCreate) ? options.toCreate : model.schema.indexes();\n  let indexError;\n\n  options = options || {};\n  const done = function(err) {\n    if (err && !model.$caught) {\n      model.emit('error', err);","sourceCodeStart":1668,"sourceCodeEnd":1704,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L1668-L1704","documentation":"Model.createIndexes() is an alias that forwards to ensureIndexes(); it shares the same Mongoose-7 callback removal, throwing when either argument is a function. The throw happens in createIndexes itself, before delegation.","triggerScenarios":"`User.createIndexes(cb)`; `User.createIndexes({}, cb)`; code written against the MongoDB driver's callback-era API applied to a mongoose model.","commonSituations":"Confusion between the native driver's createIndexes and mongoose's model static; migration leftovers.","solutions":["Await it: `await User.createIndexes();` (or call ensureIndexes, they are equivalent)","Replace driver-style `collection.createIndex(spec, cb)` with the promise form if mixing layers","Consolidate on autoIndex/Model.init() so manual index calls disappear"],"exampleFix":"// before\nUser.createIndexes((err) => { ... });\n\n// after\nawait User.createIndexes();","handlingStrategy":"validation","validationCode":"const isFn = (a) => typeof a === 'function';\nif (isFn(options)) throw new TypeError('createIndexes() is promise-only');\nawait User.createIndexes(options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick one name (ensureIndexes) for manual calls so the alias doesn't hide in greps","TypeScript signatures make driver-style callback calls fail to compile"],"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"}