编辑/回答:这在猫鼬> = 3.0中也可以正常工作
我有一个看起来像这样的猫鼬模式:
mongoose = require 'mongoose'
ObjectId = mongoose.Schema.ObjectId
VehicleSchema = new mongoose.Schema
make: String
model: String
year: Number
body: String
fuel: String
transmission: String
parts: [ { type: ObjectId, ref: 'Part' } ]
VehicleSchema.virtual('display_name').get ->
return this.make + ' ' + this.model + ' ' + this.year
VehicleModel = mongoose.model 'Vehicle', VehicleSchema
module.exports =
Schema: VehicleSchema
Model: VehicleModel
我像这样向车辆添加“零件”:
Vehicle.Model.findById id, (err, vehicle) ->
unless err
part = new Part.Model {
...details
}
part.save (err, doc) ->
unless err
vehicle.parts.push doc
vehicle.save()
else
console.error err
这似乎有效:
{ "_id" : ObjectId("5027bd4340b00b897a000002"), "body" : "car", "fuel" : "unleaded", "make" : "stackover", "model" : "modelname", "parts" : [ ObjectId("5027bd5140b00b897a000006") ], "transmission" : "manual", "year" : 21212 }
但是当我尝试填充这些部分时:
Vehicle.Model
.findById(vehicle_id)
.populate('parts')
.exec (err, doc) ->
if err
console.error err
else
console.error doc
我收到一个错误:
TypeError: Property 'model' of object { parts: [] } is not a function at model.populate [as _populate]
是什么赋予了?我有另一个模型/控制器组合,它几乎是这个完美工作的副本(我几乎找到/替换了名词,但它仍然中断,严重吓坏了!)