“this”在下面的代码中代表“this.model('Animal')”和“this.type”是什么?
嘿,我是新的mongoose和节点。我有点混淆在下面的代码:
var animalSchema = new Schema({ name: String, type: String }); animalSchema.methods.findSimilarTypes = function (cb) { return this.model('Animal').find({ type: this.type }, cb); } var Animal = mongoose.model('Animal', animalSchema); var dog = new Animal({ type: 'dog' }); dog.findSimilarTypes(function (err, dogs) { console.log(dogs); });
this
实例代表在this.model('Animal')
和this.type
在下面的代码?
在像findSimilarTypes
this
的实例方法中, this
是数据库中文档的模型实例。
所以this.model('Animal')
正在查找Animal
模型,而this.type
是调用findSimilarTypes
时使用的模型实例的type
属性。