英文:
Mongoose - How can I throw more than one error in pre (save/update) middleware?
问题
I have some pre-save and update hooks in a model, and I need to show all the errors of validations at the same time.
There is in the documentation this information about the "next" function:
Calling next() multiple times is a no-op. If you call next() with an error err1 and then throw an error err2, mongoose will report err1.
See the reference here.
I want to do something like the code below to return two or more validation errors, but like the documentation, only the first error is thrown.
Schema.pre('save', function(next) {
if (this.prop1 == 'foo')
next(new Error('Error one'))
if (this.prop2 == 'bar')
next(new Error('Error two'))
})
How can I do it? Is there an alternative?
英文:
I have some pre save and update hooks in a model and I need to show all the errors of validations at same time.
There is in the documentation this information about next function:
> Calling next() multiple times is a no-op. If you call next() with an error err1 and then throw an error err2, mongoose will report err1.
See the reference here
I want to do something like the code below to return two or more validation errors, but like the documentation only the first error is thrown
<!-- language: lang-js -->
Schema.pre('save', function(next) {
if (this.prop1 == 'foo')
next(new Error('Error one'))
if (this.prop2 == 'bar')
next(new Error('Error two'))
})
<!-- end snippet -->
How can I do it? Is there an alternative?
答案1
得分: 2
你可以将你的错误添加到一个数组中,然后在最后,如果数组的长度大于0,你可以通过连接错误来发送一个错误消息。
Schema.pre("save", function(next) {
let validationErrors = [];
if (this.prop1 == "foo") validationErrors.push("Error one");
if (this.prop2 == "bar") validationErrors.push("Error two");
if (validationErrors.length > 0) {
next(new Error(validationErrors.join(",")));
}
next();
});
但通常我们不使用这种类型的验证。如果你已经在使用mongoose,你可以使用它的验证功能。
一些其他的验证包括:
英文:
You can add your errors into an array, and then at the end if the length of the array is greater than 0, you can send one error message by joining errors.
Schema.pre("save", function(next) {
let validationErrors = [];
if (this.prop1 == "foo") validationErrors.push("Error one");
if (this.prop2 == "bar") validationErrors.push("Error two");
if (validationErrors.length > 0) {
next(new Error(validationErrors.join(",")));
}
next();
});
But generally we don't use this kind of validation. If you are already using mongoose, you can use its validation features.
Some of the other validation packages are:
答案2
得分: 0
你好,丹尼尔,欢迎来到Stack Overflow!
我的方法是将错误保存到可迭代对象中,然后将可迭代对象作为错误对象发送(或者如果只接受字符串,可以使用join()
进行字符串化整个内容)。
请检查是否处理了您的问题,我不知道有任何内置的方法可以实现您的最终目标。
Schema.pre('save', function(next) {
const errors = [];
if (this.prop1 == 'foo')
errors.push('Error one');
if (this.prop2 == 'bar')
errors.push('Error two');
if (this.prop1 == 'foo' || this.prop2 == 'bar') next(new Error(errors))
})
英文:
Hello Daniel welcome to Stack Overflow!
My approach would be to save the errors into an iterable and send down the iterable as the error object (or stringify with join()
the whole thing if it accepts strings only.
Please check if this handles your issue, I don't know of any built-in ways of achieving your end result.
Schema.pre('save', function(next) {
const errors = [];
if (this.prop1 == 'foo')
errors.push('Error one');
if (this.prop2 == 'bar')
errors.push('Error two');
if (this.prop1 == 'foo' || this.prop2 == 'bar') next(new Error(errors))
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论