You upgraded to Mongoose 9, or copied a hook from an old answer, and now every save() rejects with TypeError: next is not a function. The fix is almost always the same: in Mongoose 9 a pre hook does not receive next at all. Make the function async, delete the next() calls and throw instead of calling next(err).

Mongoose pre hook: broken async function with next versus fixed version

Not every occurrence of this error is the Mongoose 9 one, though. A post hook with the wrong signature throws the same message on every version, and an arrow function sends you down the wrong path entirely. I reproduced each case below on Mongoose 8.24.11 and 9.13.1 (Node 24, local mongod), so the error text you see here is what you will see on your screen. If you want the fundamentals first, start with Understanding Mongoose Pre and Post middleware hooks.

Quick answer

Find the hook in your stack trace and compare it with this pair.

javascript
// Broken on Mongoose 9: TypeError: next is not a function
userSchema.pre('save', async function (next) {
  this.email = this.email.toLowerCase();
  next();
});

// Fixed: no next parameter, no next() call
userSchema.pre('save', async function () {
  this.email = this.email.toLowerCase();
});

Which section to read depends on what the hook looks like:

What your hook looks like Mongoose 8 Mongoose 9 Read
pre('save', function (next) { ... next() }) works next is not a function Cause 2
pre('save', async function (next) { ... next() }) works next is not a function Cause 1
pre('save', ...) with next(err) for errors works next is not a function Error handling
post('save', function (next) { ... }) next is not a function next is not a function FAQ
pre('findOneAndUpdate', () => { this.getUpdate() }) this.getUpdate is not a function same Cause 4

Cause 1: an async function that still calls next()

This is the most common version, and it went unnoticed because Mongoose 8 forgave it. An async function always returns a promise, so Mongoose already knows when it has finished. Calling next() on top of that is redundant. On 8 the redundancy was harmless: I ran a hook that called next() and then kept working for 50 ms, and the hook body ran exactly once with a saved document at the end. No double execution, no warning.

Mongoose 9 removed the parameter, so the same code now fails at the first next(). Here is the classic place people hit it, a password hash with bcrypt:

javascript
// Broken on Mongoose 9
userSchema.pre('save', async function (next) {
  if (!this.isModified('password')) return next();
  this.password = await bcrypt.hash(this.password, 12);
  next();
});

There are two next() calls here and both are wrong, but the early-return one is the sneaky one. On a save where the password has not changed, the guard fires, and the user sees next is not a function on a profile update that never touched passwords. The fix is a bare return:

javascript
// Fixed
userSchema.pre('save', async function () {
  if (!this.isModified('password')) return;
  this.password = await bcrypt.hash(this.password, 12);
});

I ran this version on 9.13.1 against a real database: the stored value starts with $2b$12$ and compare() returns true for the original password. A hook with no arguments and no async at all also works, if there is nothing to await.

Cause 2: you upgraded to Mongoose 9

The Mongoose 9 migration guide lists it as the first breaking change: pre middleware no longer supports next(). Anything that did async work through callbacks has to be rewritten as a promise. The mechanical part of the migration is this:

Mongoose 8 pattern Mongoose 9 replacement
function (next) { ...; next(); } function () { ... } or async function () { ... }
function (next) { next(err); } function () { throw err; }
function (next) { cb(err => next(err)); } async function () { await promisify(cb)(); }
pre('save', true, function (next, done) { ... }) rewrite as a single async function. isAsync middleware is gone

A callback-based hook is the one that needs thought, because you cannot just delete the next(). If the work happens inside a callback, the hook returns before the work is done and Mongoose moves on. Wrap the callback API in a promise and await it:

javascript
const { promisify } = require('node:util');
const audit = promisify(legacyAudit); // legacyAudit(doc, cb)

// Mongoose 8
userSchema.pre('save', function (next) {
  legacyAudit(this, err => (err ? next(err) : next()));
});

// Mongoose 9
userSchema.pre('save', async function () {
  await audit(this);
});

Three things worth doing before you bump the version:

  1. Search for every .pre( in the repository and look at the first parameter of the handler. A one-line grep -rn "pre(" src/ finds the hooks, but not multi-line ones, so read the results rather than trusting a regex.
  2. Check your plugins. A plugin that registers its own pre hook with next breaks exactly like your code does, only the fix lives in someone else’s package. Look for a Mongoose 9 release of the plugin before you upgrade. Common Pitfalls with Mongoose Plugins shows how plugin hooks interact with yours.
  3. Mongoose 9 needs Node.js 20.19.0 or newer. If the upgrade also fails at install time, that is the reason.

It fails loudly. I went looking for a silent no-op, the kind where next() is ignored and the save carries on, and did not find one on 9.13.1. Every pre hook that called next rejected the save() promise with a TypeError.

Cause 3: arrow functions in schema.pre() and schema.post()

This one deserves a correction, since the search results for it are muddled. An arrow function does not cause next is not a function. I tested schema.pre('save', (next) => { next(); }) on Mongoose 8 and it worked, because an arrow function receives its parameters like any other function. On 9 it fails for the same reason a normal function does: cause 2.

What an arrow function breaks is this. Mongoose sets this to the document (or the query) when it calls your hook, and an arrow function ignores that and keeps whatever this it was created with. The result depends on your module system:

javascript
userSchema.pre('save', async () => {
  this.password = 'x';
});
  • CommonJS: this is module.exports, so the assignment succeeds and does nothing. The document is saved untouched. This is the worst outcome because nothing fails.
  • ES modules: this is undefined and you get TypeError: Cannot set properties of undefined (setting 'password').
  • Query middleware: this.getUpdate() throws TypeError: this.getUpdate is not a function, which is the error people end up searching for next to this one.

Use function () {} for every hook that touches this. The arrow form is only safe for a post hook that uses just its arguments, for example post('save', async doc => { await notify(doc.email); }).

Cause 4: pre(‘findOneAndUpdate’) and query middleware

Mongoose has several kinds of middleware, and save is only one of them. findOneAndUpdate, updateOne, find and friends are query middleware, and this inside those hooks is a Query, not a document. I logged it: this.constructor.name is Query, and this.password is undefined.

That explains a second family of errors. The usual route in is to copy a pre('save') hook, change the event name and keep this.password and next(). The next() now fails on Mongoose 9 and the this.password was never going to work. The query version reads the update through the query API:

javascript
userSchema.pre('findOneAndUpdate', async function () {
  const update = this.getUpdate();

  if (update.$set?.password) {
    update.$set.password = await bcrypt.hash(update.$set.password, 12);
  }

  this.set({ lastUpdatedMs: Date.now() });
});

await User.findOneAndUpdate(
  { email: '[email protected]' },
  { $set: { password: 'new-secret' } },
  { new: true }
);

getUpdate() returns the update object Mongoose is about to send, so you can edit it in place. this.set() adds fields to it. I ran this exact hook on both versions: the password came back transformed and lastUpdatedMs was a number.

Two limits are worth knowing. A pre('save') hook does not fire for findOneAndUpdate, so a password hash placed only on save leaves the update path unprotected. And updateOne registers as query middleware by default, so doc.updateOne() and Model.updateOne() both give you a Query in this. If you want this to be the document, register it with { document: true, query: false }.

Error handling without next(err)

The old way to stop a save was return next(new Error('...')). On Mongoose 9 that line is itself the bug: the next is missing, so you get TypeError: next is not a function and your intended error vanishes. The validation message you wrote never reaches the logs, which makes the failure look like a Mongoose problem rather than a rejected save.

Throw instead. In an async hook a thrown error rejects the promise, Mongoose aborts the chain and the save() call rejects with your error:

javascript
userSchema.pre('save', async function () {
  if (await this.constructor.exists({ username: this.username, _id: { $ne: this._id } })) {
    const err = new Error('username already taken');
    err.status = 409;
    throw err;
  }
});

try {
  await new User({ username: 'manisuec' }).save();
} catch (err) {
  console.log(err.message, err.status); // username already taken 409
}

Custom properties such as status survive, which is what you want for an Express error handler. For more on shaping those, see Node.js Error Handling Patterns for Production.

Error-handling post middleware is the exception to everything above. A hook with three parameters, (err, doc, next), still gets a real next, and on 9.13.1 I could wrap an error with next(new Error('wrapped: ' + err.message)) and the caller received the wrapped one. The post hook is also the one place where next is optional: post('save', async function (doc) { ... }) works and so does post('save', function (doc, next) { next(); }).

Frequently Asked Questions

Can I still use next() in synchronous Mongoose middleware? Not in a pre hook on Mongoose 9. A synchronous hook with no parameters works: schema.pre('save', function () { this.name = this.name.trim(); }) runs and the save continues. On Mongoose 8 the next callback still works, but you are only deferring the rewrite.

Why does next is not a function happen in post('save') hooks? Because the first argument of a post hook is the document, not next. post('save', function (next) { next(); }) calls the saved document as a function, and it fails on every Mongoose version. Write function (doc, next) if you need next, or function (doc) and skip it.

How do I pass errors out of an async Mongoose pre-hook? Throw. throw new Error('...') rejects the promise Mongoose is waiting on, aborts the remaining hooks and rejects the save() or query that triggered it. You can attach your own properties, like status, and read them in the catch.

Does Mongoose 8 still support next()? Yes. Mongoose 8 passes next to pre hooks and also accepts a returned promise. Writing hooks without next now means they run unchanged when you upgrade.

My hook has no next and it is still not running. Why? Hooks added after mongoose.model() are ignored, and the arrow-function this problem can make a hook look like it did nothing. Register all middleware on the schema before compiling the model.