Skip to content

fix: next function is not available for pre middlewares in mongoose 9 - #171

Merged
dsanel merged 2 commits into
dsanel:masterfrom
abnud11:fix/support-mongoose-9
Dec 16, 2025
Merged

dsanel merged 2 commits into
dsanel:masterfrom
abnud11:fix/support-mongoose-9

Conversation

@abnud11

@abnud11 abnud11 commented Nov 24, 2025

Copy link
Copy Markdown
Contributor

When trying to upgrade Mongoose in our app to 9, we faced this error:

TypeError: next is not a function

That's because Mongoose 9 no longer passes next to pre middlewares; instead, you either throw an error or do nothing.

This PR attempts to fix the error while still passing the tests.

Copilot AI review requested due to automatic review settings November 24, 2025 11:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR upgrades the mongoose-delete plugin to support Mongoose 9.x while maintaining backward compatibility with versions 5-8. The main change addresses Mongoose 9's removal of the next callback parameter from pre middlewares by conditionally checking if next is a function before calling it.

  • Adds Mongoose 9.x to peer dependencies
  • Updates dev dependency to Mongoose ^9.0.0
  • Fixes pre('save') middleware to work with both old and new Mongoose versions

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
package.json Adds Mongoose 9.x to peer dependencies and updates dev dependency to ^9.0.0
package-lock.json Updates dependency tree with Mongoose 9.0.0 and its transitive dependencies (mongodb 7.0.0, kareem 3.0.0, etc.)
index.js Adds conditional check for next function in pre('save') middleware to support Mongoose 9's removal of callback parameter, plus code formatting improvements
Comments suppressed due to low confidence (3)

index.js:347

  • This expression assigns variable conditions to itself.
        callback = adjustCallbackForMongooseVersion(callback);

index.js:211

  • This negation always evaluates to true.
                    schema.statics[method + 'Deleted'] = function () {

index.js:217

  • This negation always evaluates to true.
                    };

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@abnud11

abnud11 commented Dec 16, 2025

Copy link
Copy Markdown
Contributor Author

@dsanel Can we review this PR please? it's necessary for mongoose 9 support

@dsanel
dsanel merged commit 0bf5d43 into dsanel:master Dec 16, 2025
30 checks passed
@dsanel

dsanel commented Dec 16, 2025

Copy link
Copy Markdown
Owner

@abnud11 thx. Added an extra commit [164d644] to add additional check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants