我是一个很差的学习者。
我写了一个Schema并保存在Controller类中。在Schema中,我定义了验证错误消息,如下所示
var userSchema = new Schema( {
name: {type: String, required : 'please insert name'}
});
User = mongoose.model('User', userSchema);
module.exports = {
User : User
};
将其保存在控制器类中并打印错误
insertUser.save(function (err,user){
if(err) {
console.log(err);
res.send(err);
}
else res.send("inserted user name : " + user.name);
});
但我发现错误消息
{"message":"Validation failed","name":"ValidationError","errors":{"name": {"message":"Validator \"required\" failed for path name with value `undefined`","name":"ValidatorError","path":"name","type":"required"}}}
为了显示正确的验证消息,我做了哪些变更?
1条答案
按热度按时间dgtucam11#