The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

hettlage (136) [Avatar] Offline
#1
The text notes that "[a] pre hook on save is run anytime the user is saved. That means on creation and after an update.". This might be a bit misleading, as it is not called when using update or findOneAndUpdate. Quoting from http://mongoosejs.com/docs/middleware.html: "Pre and post save() hooks are not executed on update(), findOneAndUpdate(), etc. You can see a more detailed discussion why in this GitHub issue. Mongoose 4.0 has distinct hooks for these functions."
TheWexler (62) [Avatar] Offline
#2
Yes and no. You are correct in that the update methods themselves do not invoke the pre-save hook. When I wrote that line I intended using the `save` method on a User instance to update the user's information. I will need to reword the text.

In other words, in the following two examples the pre-save hook on the User model would run:

 
var user;
User.create({name: {first: 'Jon', last: 'Wexler '}, email: 'jon@jonwexler.com', password: 'pass123'}).then(u => user = u);


And

 
use.email = 'info@jonwexler.com';
user.save();


I'll make that more clear in that section. Thanks for pointing that out.