You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
replace .env.example with .env and put your mongodb connection string
run npm install
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
The text was updated successfully, but these errors were encountered:
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:
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
Is there an existing issue for this?
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
npm install
npm run start:dev
launch the creation of a "test" document:
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?
Other
No response
The text was updated successfully, but these errors were encountered: