Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

init and save hooks not firing for EmbeddedDocuments in a single nested schema #3680

Closed aliatsis closed 8 years ago

aliatsis commented 8 years ago

hooks The .pre('save') hook works fine, but the .post('save') hook doesn't fire for me.

var childSchema = Schema({ count: Number });

childSchema.pre('init', function(next) {
// never called
});

childSchema.pre('save', function(next) {
  // never called
});

childSchema.post('save', function(next) {
  // never called
});

var SingleNestedSchema = new Schema({
  children: [childSchema]
});

var ParentSchema = new Schema({
  singleNested: SingleNestedSchema
});

mongoose.model('Parent', ParentSchema);
vkarpov15 commented 8 years ago

Is this the same issue as #3679 or is there something different?

aliatsis commented 8 years ago

@vkarpov15 they are different in that pre('init') seems to work for a single nested schema but not for embedded documents within a single nested schema.

aliatsis commented 8 years ago

also pre('save') works on a single nested schema as well but not for embedded documents within a single nested schema.

vkarpov15 commented 8 years ago

Ah ok now I understand. Thanks for reporting and clarifying, will fix today.

aliatsis commented 8 years ago

awesome! thanks for the quick fix!

aliatsis commented 8 years ago

@vkarpov15 not sure if this is an issue or not so I didn't want to create a new ticket.

What is the expected order of the hooks in this case? I put logs in my hooks and received the following:

pre save: single nested
pre save: embedded
pre save: parent

post save: parent
post save: single nested
post save: embedded
post save: embedded
post save: single nested

Shouldn't pre-save be bottom-up (e.g. embedded > single nested > parent) and post-save be top-down (e.g. parent > single nested > embedded)? post-save does seem to do this with the exception of the last 2 logs (not sure what's up with that).

Thanks again!