Skip to content

Instantly share code, notes, and snippets.

@danared
Created March 27, 2015 18:56
Show Gist options
  • Save danared/9d5a3fdaab4f6f9ce2c6 to your computer and use it in GitHub Desktop.
Save danared/9d5a3fdaab4f6f9ce2c6 to your computer and use it in GitHub Desktop.
var bandSchema = new mongoose.Schema({
name: String,
lead: { type: mongoose.Schema.Types.ObjectId, ref: 'person' }
});
var autoPopulateLead = function(next) {
this.populate('lead');
next();
};
bandSchema.
pre('findOne', autoPopulateLead).
pre('find', autoPopulateLead);
var Band = mongoose.model('band', bandSchema, 'bands');
@mathieug
Copy link

Why don't you pass next as second parameter of this.populate('lead'); (L7)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment