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
Post init event does not fire after document creation #2363
Comments
Not sure, I'll investigate |
I use version 3.8.17 |
Hmm have you tried using the doc parameter?
I'm not sure if |
Да, Валерий. I've tried to use that you said. But nothing happens. // CardType.js
CardType.post( 'init', function( doc ){
doc._original = doc.toObject();
console.log( 'post init', doc.name );
}); There was no console output. So in my case post init handler did not fire at all. |
Hmm perhaps an exception is happening? Try putting the Also, you might want to try upgrading to 3.8.18 - there was a minor bug related to |
Result is the same, there is no changes in Ok. I will show you all the real code: // plugins/state.js
module.exports = function( schema ){
schema.post( 'init', function( doc ){
doc._original = doc.toObject();
doc._isNew = doc.isNew;
});
schema.post( 'remove', function(){
this._isRemoved = true;
});
}; // CardType.js
var Schema = require( 'mongoose' ).Schema,
util = require( '../util' ),
statePlugin = require( './plugins/state' ),
CardType = new Schema({
name: {type: String, required: true},
issuerId: {type: Schema.Types.ObjectId, ref: 'Issuer', index: true, required: true},
magneticStripe: {type: Boolean, default: false},
cardNumber: {type: Boolean, default: false},
cardNumberLength: {type: Number, default: 0},
userName: {type: Boolean, default: false},
chip: {type: Boolean, default: false},
nfc: {type: Boolean, default: false}
});
CardType.plugin( statePlugin );
CardType.post( 'save', function( doc ){
if ( doc._original && doc._original.name !== doc.name )
this.model( 'Card' ).update( {typeId: doc._id}, {typeName: doc.name}, {multi: true}, util.noop );
});
module.exports = CardType; // issuer.js
async.series({
update: function( cb ){
util.mixin( issuer, issuerData );
issuer.save( cb );
},
cardTypes: function( cb ){
CardType.create( cardTypes, cb );
}
}, function( error, result ){
// ...
}); But post init handler works fine when I write just: var type = new CardType;
type.name = 'test';
type.issuerId = ObjectId.createFromTime( Date.now() );
type.save( handler ); Thanks. |
Turns out this is unfortunately expected behavior right now - |
Similar to the code in the original issue above I was storing a copy of the existing document in the post As @ioncreature mentioned in his original post, this all used to work. Once I upgraded to 4.0 I received the same error message My original code
The problem with the new post Changed to
But in the pre |
Please open up a separate issue for that, that is an unrelated issue. |
Is there a workaround for this issue? I need a way to apply some |
@ClaytonSmith |
@vkarpov15 Please, update your last comment with a proper example of Also, I'd like to note that it's synchronous only. |
@jeremejevs thanks for pointing this out, fixed 👍 |
I'm going to close this for now, |
Here is CardType.js
I expect that all documents will have
_original
fielddoc._original
always returnundefined
Is it expected behavior or bug?
The text was updated successfully, but these errors were encountered: