简体   繁体   English

我在验证 mongodb 中的字段时出错,nodejs

[英]I got error while validating field in mongodb, nodejs

These are the errors I got from backend while validating in model and received in 'err' variable.这些是我在 model 中验证时从后端得到的错误,并在“err”变量中收到。 If I console.log(err), then it shows following errors.如果我使用 console.log(err),那么它会显示以下错误。

Error [ValidationError]: employees validation failed: fullName: This field is required from model, email: This field is required from model
    at ValidationError.inspect (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validation.js:61:24)
    at formatValue (internal/util/inspect.js:703:31)
    at inspect (internal/util/inspect.js:272:10)
    at formatWithOptions (internal/util/inspect.js:1887:40)
    at Object.Console.<computed> (internal/console/constructor.js:284:10)
    at Object.log (internal/console/constructor.js:294:61)
    at handleValidationError (D:\Programming\MERN\CRUD\routes\/employeeRoute.js:46:13)
    at D:\Programming\MERN\CRUD\routes\/employeeRoute.js:33:17
    at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:4915:16
    at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\helpers\promiseOrCallback.js:16:11
    at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:4938:21
    at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\model.js:492:16
    at D:\Programming\MERN\CRUD\node_modules\kareem\index.js:246:48
    at next (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:167:27)
    at next (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:169:9)
    at Kareem.execPost (D:\Programming\MERN\CRUD\node_modules\kareem\index.js:217:3) {
  errors: {
    fullName: MongooseError [ValidatorError]: This field is required from model
        at new ValidatorError (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1178:13)
        at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1161:7
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1106:14)
        at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\document.js:2378:18
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      properties: [Object],
      kind: 'required',
      path: 'fullName',
      value: '',
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    email: MongooseError [ValidatorError]: This field is required from model
        at new ValidatorError (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1178:13)
        at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1161:7
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (D:\Programming\MERN\CRUD\node_modules\mongoose\lib\schematype.js:1106:14)
        at D:\Programming\MERN\CRUD\node_modules\mongoose\lib\document.js:2378:18
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      properties: [Object],
      kind: 'required',
      path: 'email',
      value: '',
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'employees validation failed'
}

Now if I console.log('err.errors.fullName.message').现在,如果我 console.log('err.errors.fullName.message')。 It works perfectly.它工作得很好。 But if there are more keys, it's obvious that we need to use loop.但是如果key比较多,显然我们需要使用循环。 So if I try to iterate through keys of object as follow, then it throws following error.因此,如果我尝试按以下方式遍历 object 的键,则会抛出以下错误。

for(field in err.errors){
.......
.......
}

Error is:错误是:

ReferenceError: field is not defined

What's the problem here?这里有什么问题?

First of all your backend should not give this type of error response as you mentioned in the above.首先,您的后端不应像上面提到的那样提供此类错误响应。 This must be like a JSON response of errors.这必须类似于 JSON 错误响应。 eg例如

{ success: false, errors: [{fullName: "fullName is required"}] }

Coming to your question.来回答你的问题。 If you want to show error properly you can loop through the error which you can pic by the err.errors如果你想正确显示错误,你可以遍历错误,你可以通过err.errors图片

eg err.errors.forEach(error => console.log(error) )

But again this is a very wrong way to throw the validation error.但这又是一种非常错误的抛出验证错误的方法。 To validate any data you can use joi, request-validator, validatorjs, etc. Which will give you a perfect error response as you want?要验证任何数据,您可以使用 joi、request-validator、validatorjs 等。哪个会根据需要为您提供完美的错误响应? Why I am giving stress on this because you are exposing entire directory, files, model name, etc. I hope this may help you to solve your problem为什么我要强调这一点,因为你公开了整个目录、文件、model 名称等。我希望这可以帮助你解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM