Mongoose find and findOne middleware not working Mongoose find and findOne middleware not working mongoose mongoose

Mongoose find and findOne middleware not working


The issue ended up being the order of the items. Apparently you must define the model after setting up the find hooks. This is not required for the save and update hooks though.

// define the schema for our recs modelvar recSchema = mongoose.Schema({  dis: String,  // rec display  mod: String   // modified date (HAYSTACK FORMAT)}, {  strict: false});recSchema.pre('save', function(next) {  this.mod = HDateTime.now(HTimeZone.UTC).toZinc();  next();});recSchema.pre('update', function(next) {  this.mod = HDateTime.now(HTimeZone.UTC).toZinc();  next();});recSchema.pre('find', function(next) {  console.log("Pre Find");  next();});recSchema.pre('findOne', function(next) {  console.log("Pre Find One");  next();});recSchema.post('find', function(doc) {  console.log("Post Find");});recSchema.post('findOne', function(doc) {  console.log("Post Find One");});// create the model for recsvar model = recsdb.model('recs', recSchema);