Given this simple snippet, I can easily append to my find
query an additional parameter of iid
. How might you accomplish this same type of hook inside of a middleware?
// SchemaFile.js
import mongoose, {Schema} from 'mongoose'
export const ServiceSchema = new Schema({
displayName: String,
iid: String
})
// This works great, but `iid` needs to be dynamic
ServiceSchema.pre('find', function(done) {
this.find({iid: 'hard_coded'})
done()
})
// SomeMiddleware.js
import {ServiceSchema} from 'SchemaFile'
app.use(function(req, res, next) {
ServiceSchema.pre('find', function(done) {
this.find({iid: res.locals.iid})
done()
})
next()
})
I have found that the middleware never actually runs the pre hook when setup like this. Is this sort of thing possible?