繁体   English   中英

填充时出现猫鼬错误(“模型”不是函数)

[英]Mongoose Error when Populating ('model' is not a function)

编辑/答案:在猫鼬> = 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]

是什么赋予了? 我还有另一种模型/控制器组合,几乎是该模型/控制器的完美复制(我已经找到/替换了名词,但它仍然坏掉了,在这里很奇怪!)

我认为名称“模型”可能在内部使用,而VehicleSchema的定义正在掩盖猫鼬期望是“字符串”类型的函数的某些东西。

模式中各parts的定义看起来很简单。 应该不是refsref:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM