2016-07-06 56 views
2

我的架构如下:具有架构违规MongoDB中的自定义验证消息和的NodeJS

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 } } } 

所以,上面的回应有问题:​​

  1. 'name must not empty''is not coming anywhere
  2. 值来为未定义

有没有办法来改变message: 'Validator "required" failed for path name with value未定义'?什么是正确的方法?

回答

1

问题是我使用的是老版本的猫鼬。更新后,它工作正常。更新版本到4.5.3

+0

需要在这个答案中提到新版本x.x.x。只是为了避免投票下来 –