= 3.0 as well I have a mongoose diagram t...">

Mongoose error when populating ("model" is not a function)

EDIT / ANSWER: this works fine in mongoose> = 3.0 as well

I have a mongoose diagram that looks like this:

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 

I add "details" to the vehicle as follows:

 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 

which seems to work:

 { "_id" : ObjectId("5027bd4340b00b897a000002"), "body" : "car", "fuel" : "unleaded", "make" : "stackover", "model" : "modelname", "parts" : [ ObjectId("5027bd5140b00b897a000006") ], "transmission" : "manual", "year" : 21212 } 

but when I try to fill in the parts:

 Vehicle.Model .findById(vehicle_id) .populate('parts') .exec (err, doc) -> if err console.error err else console.error doc 

I get an error message:

 TypeError: Property 'model' of object { parts: [] } is not a function at model.populate [as _populate] 

What gives? I had another model / controller combination that was almost a copy of this that worked perfectly (I find / replace nouns quite a lot and it still breaks, seriously worrying here!)

+4
source share
2 answers

I think its likely that the name "model" is used internally, and the definition of VehicleSchema knocks out something that the mongoose expects to be a function of type "String".

+3
source

The definition of parts in the schema is disabled. Shouldn't refs be ref: :?

 parts: [ { type: ObjectId, ref: "Part" } ] 
+2
source

All Articles