簡體   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