【问题标题】:Mongoose validation giving "CastError: Cast to undefined failed for value"Mongoose 验证给出“CastError: Cast to undefined failed for value”
【发布时间】:2015-08-10 10:54:40
【问题描述】:

当我试图将数据保存在数据库中时,它给出了禁止消息

CastError: Cast to undefined failed for value

[
   {"product":{"supplierId":{"undefined":"rfytr"}}},
   {"product":{"supplierId":{"$empty":"rfytr"}}}
] at path "condition"

我无法找出问题所在。 我的架构是

var mongoose = require('mongoose'),
    validator = require('mongoose-validators'),
    Schema = mongoose.Schema;
var RuleSchema = new Schema({
     description: {
        type: String,
        validate: [validator.isLength(1, 50)]
    },

    status: {
        type: String,
        enum: ['live', 'active', 'paused', 'inactive', 'unfinished']
    },

    /** 
      condition. It can only contain object.
    */
    condition: [{
        type: Object
    }]
});

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    我无法重现该确切错误,但您应该将架构中 condition 的定义更改为非结构化对象数组的受支持格式之一,因为我在尝试使用 @ 时遇到其他错误987654323@。这些工作中的任何一个:

    condition: [{}]
    condition: [Schema.Types.Mixed]
    

    但您还需要删除 $empty 字段名称上的前导 $ 作为 MongoDB 以 $ aren't allowed 开头的字段名称。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 2021-01-07
      • 2017-03-23
      • 2017-08-14
      • 2017-02-06
      • 1970-01-01
      • 2022-08-23
      • 2019-02-17
      相关资源
      最近更新 更多