Home > Software design >  Nodejs :Error: User validation failed: username: Path `username` is required., password: Path `passw
Nodejs :Error: User validation failed: username: Path `username` is required., password: Path `passw

Time:01-26

Error: User validation failed: username: Path `username` is required., password: Path `password` is required., email: Path `email` is required.
    at ValidationError.inspect (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\error\validation.js:50:26)
    at formatValue (node:internal/util/inspect:790:19)
    at inspect (node:internal/util/inspect:350:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2241:40)
    at formatWithOptions (node:internal/util/inspect:2103:10)
    at console.value (node:internal/console/constructor:339:14)
    at console.log (node:internal/console/constructor:376:61)
    at module.exports.createAcc (D:\Wonderfull\Projects Made\Online Shop\controllers\auth.controller.js:39:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errors: {
    username: ValidatorError: Path `username` is required.
        at validate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1330:7)       
        at D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\document.js:2905:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'username',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    password: ValidatorError: Path `password` is required.
        at validate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1330:7)       
        at D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\document.js:2905:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'password',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    email: ValidatorError: Path `email` is required.
        at validate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\schematype.js:1330:7)       
        at D:\Wonderfull\Projects Made\Online Shop\node_modules\mongoose\lib\document.js:2905:18
        at processTicksAndRejections (node:internal/process/task_queues:78:11) {
      properties: [Object],
      kind: 'required',
      path: 'email',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'User validation failed'
}

iam stuck in this for days and cant solve it so please help if u can, iam trying to create an account from form but it send empty stuffs i guess
i created my model and required all username , email and password but when i delete "required" this error disappear but create no accounts in the database

i also commented the bcrypt code because it send another error saying "Error: data and salt arguments required "

this is my model

this is my routes file

and here is my controllers controllers

this is my error

CodePudding user response:

Welcome to StackOverflow!

In your model, you have mentioned the required: true for the username, password, and email, so you need to pass the value to that required field in your front-end or in postman. or you can handle a error instead of required field like this.

if (!username|| username== "") {
    return res.send("username is required");
  }

and

"Error: data and salt arguments required" is because of you need to pass the argument data and salt value. without these two argument the method will returns a error.

here is a simple example for encrypt the password.

await bcrypt.hash(password, 10);
  • Related