Nodejs:错误:用户验证失败:用户名:需要路径“用户名”。,密码:需要路径“密码”。,电子邮件:需要路径“电子邮件”

问题描述 投票:0回答:1
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'
}

我被困在这个问题好几天了,无法解决它,所以如果可以的话请帮忙,我正在尝试从表单创建一个帐户,但我猜它会发送空的东西
我创建了我的模型并需要所有用户名、电子邮件和密码,但是当我删除“必需”时 此错误消失,但在数据库中未创建任何帐户

我还评论了 bcrypt 代码,因为它发送了另一个错误,指出“错误:需要数据和盐参数”

this is my model

this is my routes file

and here is my controllers controllers

this is my error

node.js mongodb express mongoose ejs
1个回答
0
投票

在您的模型中,您提到了

required: true
username
password
email
,因此您需要将值传递到前端或邮递员中的必填字段。或者您可以处理错误而不是像这样的必填字段。

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

"Error: data and salt arguments required"
是因为您需要传递参数
data
salt
值。如果没有这两个参数,该方法将返回错误。

这是一个加密密码的简单示例。

await bcrypt.hash(password, 10);
© www.soinside.com 2019 - 2024. All rights reserved.