question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

schema.pre('save') not triggered by MyModel.update()

See original GitHub issue

example.js:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema

mongoose.connect('mongodb://localhost/test')

var Thing = new Schema({
  title : String,
  content : String
})

Thing.pre('save', function(next){
  console.log("saving: %s (%s)", this.title, this.content)
  next()
})

var MyThing = mongoose.model('Thing', Thing)

var example = new MyThing({
  title : 'foo',
  content : 'bar'
})

example.save(function(err){
  MyThing.update({title:'foo'},{content:'baz'}, function(err, rs){
    console.log(rs)
  })  
})

Output:

$ node example.js 
saving: foo (bar)
1

The pre is triggered by the initial save, but not the update.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

83reactions
r3wtcommented, Jan 11, 2017

This is fucking bullshit. we can’t do anything async in setters. idiots.

14reactions
aheckmanncommented, Jul 16, 2011

This is not a bug. pre save is a hook that fires on instances when their save method is called, not on the model when update is called.

Model.update let’s you send atomic $set operations directly to mongodb.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose .pre('save') does not trigger - node.js - Stack Overflow
Use pre('validate') instead of pre('save') to set the value for the required field. Mongoose validates documents before saving, ...
Read more >
Mongoose v6.8.1: Middleware
Calling pre() or post() after compiling a model does not work in Mongoose in general. For example, the below pre('save') middleware will not...
Read more >
Mongoose Pre-Save Hooks (144) - YouTube
DONATE TO THE SHOW!!!Donate any amount - https://bit.ly/2XmweLcMusic commonly used on this channel:http://birocratic.com ...
Read more >
Mongoose Middleware | The Javascript
const schema = Schema({ name: String }); schema.pre('save', function() { doc === this; // true }); const Model = mongoose.model('Test', ...
Read more >
Mongoose Middleware v4.4.14
Pre and post save() hooks are not executed on update() , findOneAndUpdate() , etc. You can see a more detailed discussion why in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found