{"record":{"id":"43e48608bb77618a","repo":"Automattic/mongoose","slug":"model-init-no-longer-accepts-a-callback","errorCode":null,"errorMessage":"Model.init() no longer accepts a callback","messagePattern":"Model\\.init\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":1120,"sourceCode":" *\n * #### Example:\n *\n *     const eventSchema = new Schema({ thing: { type: 'string', unique: true } })\n *     // This calls `Event.init()` implicitly, so you don't need to call\n *     // `Event.init()` on your own.\n *     const Event = mongoose.model('Event', eventSchema);\n *\n *     await Event.init();\n *     console.log('Indexes are done building!');\n *\n * @api public\n * @returns {Promise}\n */\n\nModel.init = function init() {\n  _checkContext(this, 'init');\n  if (typeof arguments[0] === 'function') {\n    throw new MongooseError('Model.init() no longer accepts a callback');\n  }\n\n  this.schema.emit('init', this);\n\n  if (this.$init != null) {\n    return this.$init;\n  }\n\n  const conn = this.db;\n  const _ensureIndexes = async() => {\n    const autoIndex = utils.getOption(\n      'autoIndex',\n      this.schema.options,\n      conn.config,\n      conn.base.options\n    );\n    if (!autoIndex) {\n      return;","sourceCodeStart":1102,"sourceCodeEnd":1138,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L1102-L1138","documentation":"Model.init() builds the collection and its indexes and returns a promise; since Mongoose 7 the callback API was removed entirely. If `arguments[0]` is a function, init() throws immediately instead of silently dropping the callback. Use `await Model.init()` and handle errors via try/catch or .catch().","triggerScenarios":"`Event.init(err => { ... })` in code written for Mongoose 6 or earlier; passing a success handler as the first argument; tutorials/documentation predating Mongoose 7.","commonSituations":"Upgrading an application from mongoose 6.x to 7+/8+ without migrating call sites; legacy codebases where only a few index-setup calls still use callbacks.","solutions":["Await the promise: `await Event.init();`","Or chain: `Event.init().then(onReady).catch(onErr)`","Run repo-wide search for `\\.init\\(` with a following function argument and migrate all matches","Check the Mongoose 7 migration guide section 'Callback API removed' for the full list of affected functions"],"exampleFix":"// before\nEvent.init(function(err) { if (err) throw err; });\n\n// after\nawait Event.init();","handlingStrategy":"validation","validationCode":"// Lint-time equivalent: refuse callback args before calling\nconst noCallbacks = (fnName, args) => {\n  if (args.some(a => typeof a === 'function')) {\n    throw new TypeError(`${fnName}() accepts no callback — await the returned promise`);\n  }\n};\n// usage: noCallbacks('init', [process.env.INIT_CB]); await Event.init();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Migrate all mongoose calls to async/await in one mechanical pass before upgrading to Mongoose 7+","Rely on TypeScript: the modern @types signatures have no callback overloads, so old code fails to compile","Add an ESLint no-restricted-syntax rule banning function arguments to known mongoose method names"],"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"}