Mongoose – 如何在预保存/更新中间件中抛出多个错误?

huangapple go评论78阅读模式
英文:

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(&#39;save&#39;, function(next) {
  if (this.prop1 == &#39;foo&#39;)
    next(new Error(&#39;Error one&#39;))
  
  if (this.prop2 == &#39;bar&#39;)
    next(new Error(&#39;Error two&#39;))
})

<!-- 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,你可以使用它的验证功能。

一些其他的验证包括:

  1. Express Validator
  2. Joi
英文:

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(&quot;save&quot;, function(next) {
  let validationErrors = [];

  if (this.prop1 == &quot;foo&quot;) validationErrors.push(&quot;Error one&quot;);

  if (this.prop2 == &quot;bar&quot;) validationErrors.push(&quot;Error two&quot;);

  if (validationErrors.length &gt; 0) {
    next(new Error(validationErrors.join(&quot;,&quot;)));
  }

  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:

  1. Express Validator
  2. Joi

答案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(&#39;save&#39;, function(next) {
  const errors = [];

  if (this.prop1 == &#39;foo&#39;)
    errors.push(&#39;Error one&#39;);
    
  if (this.prop2 == &#39;bar&#39;)
    errors.push(&#39;Error two&#39;);

  if (this.prop1 == &#39;foo&#39; || this.prop2 == &#39;bar&#39;) next(new Error(errors))
})

huangapple
  • 本文由 发表于 2020年1月3日 22:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定