Automattic/mongoose · error · MongooseError
Model.init() no longer accepts a callback
Error message
Model.init() no longer accepts a callback
What it means
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().
Source
Thrown at lib/model.js:1120
*
* #### Example:
*
* const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
* // This calls `Event.init()` implicitly, so you don't need to call
* // `Event.init()` on your own.
* const Event = mongoose.model('Event', eventSchema);
*
* await Event.init();
* console.log('Indexes are done building!');
*
* @api public
* @returns {Promise}
*/
Model.init = function init() {
_checkContext(this, 'init');
if (typeof arguments[0] === 'function') {
throw new MongooseError('Model.init() no longer accepts a callback');
}
this.schema.emit('init', this);
if (this.$init != null) {
return this.$init;
}
const conn = this.db;
const _ensureIndexes = async() => {
const autoIndex = utils.getOption(
'autoIndex',
this.schema.options,
conn.config,
conn.base.options
);
if (!autoIndex) {
return;View on GitHub (pinned to 49cdab0136)
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
Example fix
// before
Event.init(function(err) { if (err) throw err; });
// after
await Event.init(); Defensive patterns
Strategy: validation
Validate before calling
// Lint-time equivalent: refuse callback args before calling
const noCallbacks = (fnName, args) => {
if (args.some(a => typeof a === 'function')) {
throw new TypeError(`${fnName}() accepts no callback — await the returned promise`);
}
};
// usage: noCallbacks('init', [process.env.INIT_CB]); await Event.init(); Prevention
- 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
When it happens
Trigger: `Event.init(err => { ... })` in code written for Mongoose 6 or earlier; passing a success handler as the first argument; tutorials/documentation predating Mongoose 7.
Common situations: 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.
Related errors
- Model.syncIndexes() no longer accepts a callback
- Model.cleanIndexes() no longer accepts a callback
- Model.listIndexes() no longer accepts a callback
- Model.ensureIndexes() no longer accepts a callback
- Model.createIndexes() no longer accepts a callback
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/43e48608bb77618a.
Report an issue: GitHub.