Automattic/mongoose · error · MongooseError

Model.createIndexes() no longer accepts a callback

Error message

Model.createIndexes() no longer accepts a callback

What it means

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.

Source

Thrown at lib/model.js:1686

      resolve();
    });
  });
};

/**
 * Similar to `ensureIndexes()`, except for it uses the [`createIndex`](https://mongodb.github.io/node-mongodb-native/7.0/classes/Db.html#createIndex)
 * function.
 *
 * @param {object} [options] internal options
 * @return {Promise}
 * @api public
 */

Model.createIndexes = async function createIndexes(options) {
  _checkContext(this, 'createIndexes');

  if (typeof arguments[0] === 'function' || typeof arguments[1] === 'function') {
    throw new MongooseError('Model.createIndexes() no longer accepts a callback');
  }

  return this.ensureIndexes(options);
};


/*!
 * ignore
 */

function _ensureIndexes(model, options, callback) {
  const indexes = Array.isArray(options?.toCreate) ? options.toCreate : model.schema.indexes();
  let indexError;

  options = options || {};
  const done = function(err) {
    if (err && !model.$caught) {
      model.emit('error', err);

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Await it: `await User.createIndexes();` (or call ensureIndexes, they are equivalent)
  2. Replace driver-style `collection.createIndex(spec, cb)` with the promise form if mixing layers
  3. Consolidate on autoIndex/Model.init() so manual index calls disappear

Example fix

// before
User.createIndexes((err) => { ... });

// after
await User.createIndexes();
Defensive patterns

Strategy: validation

Validate before calling

const isFn = (a) => typeof a === 'function';
if (isFn(options)) throw new TypeError('createIndexes() is promise-only');
await User.createIndexes(options);

Prevention

When it happens

Trigger: `User.createIndexes(cb)`; `User.createIndexes({}, cb)`; code written against the MongoDB driver's callback-era API applied to a mongoose model.

Common situations: Confusion between the native driver's createIndexes and mongoose's model static; migration leftovers.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/374c5aecce0acd42. Report an issue: GitHub.