Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 显示Mongoose模型的多个验证错误_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 显示Mongoose模型的多个验证错误

Node.js 显示Mongoose模型的多个验证错误,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,如果我有这个模式: var userSchema = Schema( {name : { type: String } }); userSchema.path('name').validate(function(value) { return value.length > 4; }, 'Name is too short'); userSchema.path('name').validate(function(value) { retur

如果我有这个模式:

var userSchema = Schema(
    {name : {
        type: String
    }
});

userSchema.path('name').validate(function(value) {
    return value.length > 4;
}, 'Name is too short');

userSchema.path('name').validate(function(value) {
    return hasNoNumbers(value);
}, 'Name cannot have numbers');

var User = mongoose.model('User', userSchema);
然后,我创建一个类似这样的模型并运行验证函数:

var newUser = new User({name: '1da'});

newUser.validate(function(err) {
    console.log(err.errors.name);
})
这只记录第一条错误消息“名称太短”。但是,name属性无法满足这两个验证要求。是否有办法同时显示两条错误消息


谢谢

显然,mongoose的v3中没有实现此功能

当v4变得稳定时,我会再试一次

在此之前,本模块似乎解决了这个问题: