mongoose获取模型验证失败,而不是自定义错误消息

mongoose获取模型验证失败,而不是自定义错误消息,mongoose,Mongoose,我确信问题出在密码验证器上,因为如果我把它注释掉,文档就会被插入。此外,如果我输入有效密码,一切正常。这是我的整个用户模式 var UserSchema = new Schema({ firstName: String, lastName: String, email: { type: String, unique: true, match: /.+\@.+\..+/ }, website: {

我确信问题出在密码验证器上,因为如果我把它注释掉,文档就会被插入。此外,如果我输入有效密码,一切正常。这是我的整个用户模式

var UserSchema = new Schema({
    firstName: String,
    lastName: String,
    email: {
        type: String,
        unique: true,
        match: /.+\@.+\..+/
    },
    website: {
        type: String,
        set: urlModifier
    },
    username: {
        type: String,
        trim: true,
        required: true,
        unique: true
    },
    password: {
        type: String,
        validate: [
          function(password) {
            return password.length >= 6;
          },
        'Password should be longer'
    ]
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    role: {
        type: String,
        enum: ['admin', 'owner', 'user']
    }
});
我遇到的问题是,我得到的不是自定义错误消息,而是

ValidationError: User validation failed
<br> &nbsp; &nbsp;at model.Document.invalidate (C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1162:32)
    <br> &nbsp; &nbsp;at C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1037:16
        <br> &nbsp; &nbsp;at validate (C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:651:7)
            <br> &nbsp; &nbsp;at C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:679:9
                <br> &nbsp; &nbsp;at Array.forEach (native)
                    <br> &nbsp; &nbsp;at SchemaString.SchemaType.doValidate (C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:656:19)
                        <br> &nbsp; &nbsp;at C:\Users\lotus\Desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1035:9
                            <br> &nbsp; &nbsp;at process._tickCallback (node.js:355:11)
ValidationError:用户验证失败

在model.Document.invalidate(C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\Document.js:1162:32)
在C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\document.js:1037:16
验证时(C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\schematype.js:651:7)
在C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\schematype.js:679:9
at Array.forEach(本机)
在schematring.SchemaType.doValidate(C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\SchemaType.js:656:19)
在C:\Users\lotus\Desktop\mastering\u mean\application\node\u modules\mongoose\lib\document.js:1035:9
在进程中调用(node.js:355:11)
猫鼬版本是^4.05


不确定这是一个bug、API中的某些更改还是我做错了什么。

在我学习的特定教程中没有提到这一点

自定义错误消息在返回控制器中的save(err)函数作为err的验证错误对象中可用

基本的

user.save(函数(err){ 如果(错误){ 返回下一个(错误); }否则{ res.json(用户); } });

将返回默认错误消息“用户验证失败”

这个


将返回自定义错误消息

在我学习的特定教程中未提及此内容

自定义错误消息在返回控制器中的save(err)函数作为err的验证错误对象中可用

基本的

user.save(函数(err){ 如果(错误){ 返回下一个(错误); }否则{ res.json(用户); } });

将返回默认错误消息“用户验证失败”

这个

将返回自定义错误消息

user.save(function(err) {
        if(err) {
            console.log(err.errors.password.message);
            return next(err);
        } else {
            res.json(user);
        }
    });