Node.js 如何清除mongoose验证错误
我正在使用mongoose进行验证,我希望将结果传递回客户端。mongoose为错误提供的JSON blob几乎正是我所需要的,但它有一些额外的东西:Node.js 如何清除mongoose验证错误,node.js,mongoose,Node.js,Mongoose,我正在使用mongoose进行验证,我希望将结果传递回客户端。mongoose为错误提供的JSON blob几乎正是我所需要的,但它有一些额外的东西: errors: { username: { [ValidatorError: Path `username` is required.] properties: [Object], message: 'Path `username` is required.', name: 'ValidatorE
errors:
{ username:
{ [ValidatorError: Path `username` is required.]
properties: [Object],
message: 'Path `username` is required.',
name: 'ValidatorError',
kind: 'required',
path: 'username',
value: undefined } } }
客户端不需要
username.properties
字段。我知道我可以删除username.properties
,但是有更好的方法清理JSON吗?使用lodash
库
var _ = require('lodash');
yourMongooseError = _.mapValues(yourMongooseError, function(field) {
return _.omit(field, ['properties']); // add any other props that you want to remove from each field
});