猫鼬:无法读取未定义的_id属性“ where”

时间:2020-05-14 16:06:54

标签: mongoose unique model-validation mongoose-models

我有那个模型:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var uniqueValidator = require('mongoose-unique-validator');

var EquipmentSchema = new Schema({
    model: { type: String, required: true, unique: true},
    price: { type: Number, required: true },
    comments: { type: String },
    market: { type: mongoose.Types.ObjectId, ref: 'Market', required: true },
    category: { type: mongoose.Types.ObjectId, ref: 'Category', required: true },
    make: { type: mongoose.Types.ObjectId, ref: 'Make', required: true },
    rv: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Rv'}],
});

EquipmentSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Equipments', EquipmentSchema);

此功能可保存新项目:

const Equipments = require('./_models/equipment.model');

var equipmentSaver = function(equipment) {
  return new Promise((resolve, reject) => {
    console.log(equipment);
    const equipmentToSave = new Equipments({
      _id: equipment.type.model._id,
      market: equipment.type.market._id,
      category: equipment.type.category._id,
      make: equipment.type.make._id,
      model: equipment.type.model.name,
      price: equipment.economicDetails.price,
      comments: equipment.economicDetails.comments,
    });
    equipmentToSave.save()
      .then(result => resolve(result))
      .catch(error => reject(error))
   });
}

module.exports = equipmentSaver;

我从前面得到了JSON:

{
  type: {
    market: {
      _id: '5eb1a63926849bed71155592',
      name: 'Healthcare',
      icon: 'local_hospital'
    },
    category: {
      _id: '5eb19f080141dbe4b9aebc9a',
      name: 'Diagnostico por la imagen',
      market: '5eb1a63926849bed71155592'
    },
    make: {
      _id: '5ebd3e107002040000e70e53',
      name: 'GE',
      category: '5eb19f080141dbe4b9aebc9a'
    },
    model: {
      _id: '5ebd3e167002040000e70e54',
      name: 'Voluson S8',
      make: '5ebd3e107002040000e70e53'
    }
  },
  economicDetails: {
    comments: 'Description',
    price: 45000,
    used: false
  }
}

所有_ids都是由前端创建的,我得到这2个错误: 1。

Error [ValidationError]: Equipments validation failed: _id: Cannot read property 'where' of undefined, model: Cannot read property 'where' of undefined
    at ValidationError.inspect
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
      message: "Cannot read property 'where' of undefined",
      name: 'ValidatorError',
      properties: [Object],
      kind: 'unique',
      path: '_id',
      value: 5ebd3e167002040000e70e54,
      reason: TypeError: Cannot read property 'where' of undefined

2。

MongooseError [ValidatorError]: Cannot read property 'where' of undefined
        at new ValidatorError
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
      message: "Cannot read property 'where' of undefined",
      name: 'ValidatorError',
      properties: [Object],
      kind: 'unique',
      path: 'model',
      value: 'Voluson S8',
      reason: TypeError: Cannot read property 'where' of undefined

我不知道为什么。我觉得这与这两个字段的“唯一”字符有关,但我不确定为什么会引发此错误...

编辑:我已经找到错误的根源。当我从模型中删除EquipmentSchema.plugin(uniqueValidator);时,它会再次正确保存... 我在mongoose-unique-validator's github上创建了一个问题,但是如果这里有人遇到同样的问题,甚至有解决方案,那就太好了!

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现问题出在您其中一个字段的名称上: model

中的EquipmentSchema字段
var EquipmentSchema = new Schema({
    //this throws Cannot read property 'where' of undefined
    model: { type: String, required: true, unique: true} 
    ...
})

当您将该字段重命名为其他字段或将其删除时,该插件可以正常工作。 仍然是mongoose-unique-validator插件问题,因为model不是 在list of reserved document keys中 猫鼬。

通过查看this section of the plugin code (最新版本),您可以看到有if ... else if ...个条件可以找到 model,并像您一样定义一个名为model的字段,会使所有条件均失败,并使model变量保持未定义状态,从而最终引发错误。