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
Previously (similar to #2363) I was storing a copy of the existing doc in the post init hook, then in the pre save hook I compared the original doc with the updated doc (if not new), then in the post save hook I had logic to send a notification of the changes.
Before I upgraded to 4.0, the code below used to work (I don't recall which version of 3.8 I was using), but now I receive the error message Object #<EventEmitter> has no method 'toObject' when trying to copy the current object. I've been trying to come up with a workable solution, but I can't see a way to persist the original document through to the post save event.
My original code (3.8.x)
schema.post('init', function() {
this._original = this.toObject();
this._changes = []; //array of objects with the path, new and old values. Populated by `observe` function in pre `save` hook
});
schema.pre('save', function(next) {
//other logic here. Cut out for this example..
this.observe(); //compares the current doc with the original and stores the new and old values in `this._changes` array.
next();
});
schema.post('save', function(doc) {
if (this._original && this._changes.length > 0) {
//send web hook notification, with changes
} else {
//send web hook notification, with new document
}
//reset the values, in case the document is saved again.
this._original = null;
this._original = this;
this._changes = [];
});
The problem with the new post init architecture is that when I add the doc parameter to the hook function I can get the doc and add a copy to itself, but that copy will no longer be available in the pre or post save hooks.
Later, in the pre save hook, this._original is undefined, even for documents that we returned from the database. Any suggestions on how to reliably save a copy of the original document and have it survive the document lifecycle.
The text was updated successfully, but these errors were encountered:
This issue is another manifestation of #2949 and #2925, accidental regression in 4.0.2 that occurred when we theoretically brought back the ability to do:
schema.post('init',function(){this._original=this.toObject();this._changes=[];//array of objects with the path, new and old values. Populated by `observe` function in pre `save` hook});
You should be able to do that again in 4.0.3, the below script works for me on 4.0.3:
Previously (similar to #2363) I was storing a copy of the existing doc in the post
init
hook, then in the presave
hook I compared the original doc with the updated doc (if not new), then in the postsave
hook I had logic to send a notification of the changes.Before I upgraded to 4.0, the code below used to work (I don't recall which version of 3.8 I was using), but now I receive the error message
Object #<EventEmitter> has no method 'toObject'
when trying to copy the current object. I've been trying to come up with a workable solution, but I can't see a way to persist the original document through to the postsave
event.My original code (3.8.x)
The problem with the new post
init
architecture is that when I add thedoc
parameter to the hook function I can get thedoc
and add a copy to itself, but that copy will no longer be available in the pre or postsave
hooks.Changed to (4.0.x)
Later, in the pre
save
hook,this._original
is undefined, even for documents that we returned from the database. Any suggestions on how to reliably save a copy of the original document and have it survive the document lifecycle.The text was updated successfully, but these errors were encountered: