Node.js 在mongodb和nodejs中为架构冲突提供自定义验证消息

Node.js 在mongodb和nodejs中为架构冲突提供自定义验证消息,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我的模式如下: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var StudentSchema = new Schema({ name: { type: String, required: [true, 'name must be non empty'] }, family: { type: String,

我的模式如下:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var StudentSchema   = new Schema({
    name: {
        type: String,
        required: [true, 'name must be non empty']
    },
    family: {
        type: String,
        required: [true, 'family must be non empty']
    },
    subjects: {
        type: [String],
        validate: [{
            validator: function(val) {
            return val.length > 0;
            },
            msg: 'Continents must have more than or equal to one elements',
            errorCode: 25
        }
        ]
    },
    created: {
        type: Date,
        default: Date.now
    },
});
但是,当我发布没有名称的JSON时,我看到以下错误对象:

{ [ValidationError: Validation failed]
  message: 'Validation failed',
  name: 'ValidationError',
  errors: 
   { name: 
      { [ValidatorError: Validator "required" failed for path name with value `undefined`]
        message: 'Validator "required" failed for path name with value `undefined`',
        name: 'ValidatorError',
        path: 'name',
        type: 'required',
        value: undefined } } }
因此,上述回答存在问题:

  • “名称必须非空”不会出现在任何地方
  • 值是未定义的

  • 是否有方法更改
    消息:对于值为
    未定义
    '
    的路径名,验证程序“必需”失败?正确的方法是什么

    问题是我使用的是旧版本的mongoose。更新后,它工作正常。更新版本为4.5.3

    需要在回答中提及新版本x.x.x。只是为了避免被否决