不幸的是,它的文档不是很好(在document.js
API文档中没有提及),但是文档可以通过该
constructor字段访问其模型-我一直在使用它来记录插件中的内容,这使我可以访问他们所依附的模型。
module.exports = function readonly(schema, options) { schema.pre('save', function(next) { console.log(this.constructor.modelName + " is running the pre-save hook."); // some other pre here ... next(); });});对于您的情况,您应该能够:
IdeaSchema.pre('save', function(next) { var idea = this; function generate_slug(text) { return text.toLowerCase().replace(/[^w ]+/g,'').replace(/ +/g,'-').trim(); }; idea.slug = generate_slug(idea.title); // this now works this.constructor.findOne({slug: idea.slug}, function(err, doc) { console.log(err); console.log(doc); next(err, doc); }); //console.log(idea);});


