Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose hook calling twice #2235

Closed
2 of 4 tasks
DIDIERRobin opened this issue Aug 28, 2024 · 1 comment
Closed
2 of 4 tasks

Mongoose hook calling twice #2235

DIDIERRobin opened this issue Aug 28, 2024 · 1 comment
Labels

Comments

@DIDIERRobin
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Uppon creating a new Test Object, the save hook is called twice depending on what we do inside the hook function.
Importing another Document as a string, or as a class impact the behavior of the hook.

Minimum reproduction code

https://github.com/DIDIERRobin/mongoose-hook-bug

Steps to reproduce

it needs a mongodb instance running

  1. replace .env.example with .env and put your mongodb connection string
  2. run npm install
  3. run npm run start:dev

launch the creation of a "test" document:

curl -X POST http://localhost:3000/test

You will see the hook is called two times instead of one. And the second time, the instance is ont a document anymore, therefore this.model does not exist.
To correct the issue, follow the instruction in the comment inside test/hook.ts file.

Expected behavior

The save hook should not be called twice wether we're calling this.model() function with a string or with the name coming from the imported class.

Worse, if we add hooks like NestJS documention suggest, inside module, we should have a way of accessing History Document without triggering the save hook twice.

Package version

10.0.10

mongoose version

8.5.4

NestJS version

10.0.0

Node.js version

18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

The save hook should not be called twice wether we're calling this.model() function with a string or with the name coming from the imported class.

I couldn't reproduce your issue. It fails with either.

Worse, if we add hooks like NestJS documention suggest, inside module, we should have a way of accessing History Document without triggering the save hook twice.

It triggers twice because you insert a test document, and then within a preSave hook a history document with another test document embedded. Once you comment these lines out:

// await historyModel.create({
//   version: 1,
//   test: {
//     title: 'x',
//   },
// });

hook runs only once.

Note: I used the forFeatureAsync (as suggested in the official docs) instead of an inline TestSchema.pre('save', preSaveHook) and it works perfectly fine. There's nothing else we can do on our (NestJS side) to fix this issue as you can reproduce the exact same behavior with pure Mongoose too

@nestjs nestjs locked and limited conversation to collaborators Oct 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants