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

isNew or isModified() for post hooks #2162

Closed
avishaan opened this issue Jun 25, 2014 · 4 comments
Closed

isNew or isModified() for post hooks #2162

avishaan opened this issue Jun 25, 2014 · 4 comments

Comments

@avishaan
Copy link

For pre('save') middleware there is isNew/isModified to figure out if the document is new. I couldn't find something similar for post('save'). I am wondering is there something similar for post('save')?

@vkarpov15
Copy link
Collaborator

See #1474 and http://stackoverflow.com/questions/17099025/determine-whether-doc-is-new-or-exists-in-mongoose-post-middlewares-save . Long story short, you can do this yourself pretty easily by recording the value of isNew in pre('save'). The isNew field is something that Mongoose uses internally to keep track of 'has this document been persisted to the database yet?', so Mongoose sets it to false after save.

@ericsaboia
Copy link

This is pretty insane. Can't understand why not to put this logic inside mongoose.

@Philmod
Copy link

Philmod commented Feb 22, 2015

I agree with @ericsaboia

@vkarpov15
Copy link
Collaborator

As far as I understand it, this is trickier than its worth. Post save hooks are run after the save function, so there's no way to clean up internal state like isNew after post hooks run, unless we either add an extra "run this post hook after all other post hooks" logic, which is very janky IMO. I'm open to ideas on how this would work, but I don't have a good solution just yet.

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

No branches or pull requests

4 participants