Сохранение данных не удалось. проверка не удалась в node.js - PullRequest
0 голосов
/ 10 марта 2020

Я пытаюсь сохранить пользовательские данные в MongoDB, но при этом регистрируется ошибка: (проверка не пройдена). Я пробовал много решений, но они не работали. У вас есть какие-либо решения для этого?

Модель Mongodb / Mon goose:

const mongoose = require('mongoose')

const userSignUpSchema = new mongoose.Schema({ //This is my Schema
  fullName: {
    type: String,
    required: true

  },
  email: {
    type: String,
    required: true

  },
  phoneNumber: {
    type: Number,
    required: true

  },
  password: {
    type: String,
    required: true

  }
})
module.exports = mongoose.model('userSignUpData', userSignUpSchema);

Функция сохранения:

app.post('/sinupPostReq', async (req, res) => {
  const newUser = new userSignUpData({ //Here i Send the NewUser
    fullName: req.body.fullName,
    email: req.body.email,
    phoneNumber: req.body.phoneNumber,
    password: req.body.password
  })
  // Below i Save the newUser to the DataBase
  newUser.save().then(() => console.log('User has been successfully created')).catch(err => console.log('something went wrong', err));

  //res.redirect('/');
});

Форма Html:

<!-- THIS IS THE FORM THAT SUBMITTS THE DATA -->
<form class="" action="/sinupPostReq" method="POST"
  enctype="multipart/form-data">
  <label for="fullName" class="sr-only">Full Name</label>
  <input required type="text" name="fullName" placeholder="full name"
    id="fullName" class="form-control col my-4">
  <label for="email" class="sr-only">Email</label>
  <input required type="text" name="email" id="email" placeholder="email"
    class="form-control col my-4">
  <label for="phoneNumber" class="sr-only">Phone Number</label>
  <input required type="number" class="form-control col my-4"
    placeholder="Phone Number" name="phoneNumber" id="phoneNumber">
  <div class="row">
    <div class="col">
      <label for="password" class="sr-only">Password</label>
      <input required type="password" name="password" id="password"
        class="form-control my-4" placeholder="Password">
    </div>
  </div>
  <div class="row">
    <div class="col">
      <button type="submit" class="btn btn-success col">Signup</button>
    </div>
    <div class="col">
      <a href="/" class="btn btn-primary col">Login</a>
    </div>
  </div>
</form>

И это сообщение об ошибке:

    something went wrong Error [ValidationError]: userSignUpData validation failed: fullName: Path `fullName` is required., email: Path `email` is required., phoneNumber: Path `phoneNumber` is required., password: Path `password` is required.
    at ValidationError.inspect (E:\jsFramework\node_modules\mongoose\lib\error\validation.js:61:24)
    at formatValue (internal/util/inspect.js:693:31)
    at inspect (internal/util/inspect.js:264:10)
    at formatWithOptions (internal/util/inspect.js:1865:40)
    at Object.Console.<computed> (internal/console/constructor.js:281:10)
    at Object.log (internal/console/constructor.js:291:61)
    at E:\jsFramework\server.js:42:101
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  errors: {
    fullName: MongooseError [ValidatorError]: Path `fullName` is required.
        at new ValidatorError (E:\jsFramework\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1096:13)
        at E:\jsFramework\node_modules\mongoose\lib\schematype.js:1150:11
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1105:14)
        at E:\jsFramework\node_modules\mongoose\lib\document.js:2367:9
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      message: 'Path `fullName` is required.',
      name: 'ValidatorError',
      properties: [Object],
      kind: 'required',
      path: 'fullName',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    email: MongooseError [ValidatorError]: Path `email` is required.
        at new ValidatorError (E:\jsFramework\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1096:13)
        at E:\jsFramework\node_modules\mongoose\lib\schematype.js:1150:11
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1105:14)
        at E:\jsFramework\node_modules\mongoose\lib\document.js:2367:9
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      message: 'Path `email` is required.',
      name: 'ValidatorError',
      properties: [Object],
      kind: 'required',
      path: 'email',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    phoneNumber: MongooseError [ValidatorError]: Path `phoneNumber` is required.
        at new ValidatorError (E:\jsFramework\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1096:13)
        at E:\jsFramework\node_modules\mongoose\lib\schematype.js:1150:11
        at Array.forEach (<anonymous>)
        at SchemaNumber.SchemaType.doValidate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1105:14)
        at E:\jsFramework\node_modules\mongoose\lib\document.js:2367:9
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      message: 'Path `phoneNumber` is required.',
      name: 'ValidatorError',
      properties: [Object],
      kind: 'required',
      path: 'phoneNumber',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    password: MongooseError [ValidatorError]: Path `password` is required.
        at new ValidatorError (E:\jsFramework\node_modules\mongoose\lib\error\validator.js:29:11)
        at validate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1096:13)
        at E:\jsFramework\node_modules\mongoose\lib\schematype.js:1150:11
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (E:\jsFramework\node_modules\mongoose\lib\schematype.js:1105:14)
        at E:\jsFramework\node_modules\mongoose\lib\document.js:2367:9
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      message: 'Path `password` is required.',
      name: 'ValidatorError',
      properties: [Object],
      kind: 'required',
      path: 'password',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'userSignUpData validation failed',
  name: 'ValidationError'
}
NotFoundError: Not Found
    at E:\jsFramework\server.js:48:8
    at Layer.handle [as handle_request] (E:\jsFramework\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (E:\jsFramework\node_modules\express\lib\router\index.js:317:13)
    at E:\jsFramework\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (E:\jsFramework\node_modules\express\lib\router\index.js:335:12)
    at next (E:\jsFramework\node_modules\express\lib\router\index.js:275:10)
    at SendStream.error (E:\jsFramework\node_modules\serve-static\index.js:121:7)
    at SendStream.emit (events.js:311:20)
    at SendStream.error (E:\jsFramework\node_modules\send\index.js:270:17)
    at SendStream.onStatError (E:\jsFramework\node_modules\send\index.js:421:12)
    at onstat (E:\jsFramework\node_modules\send\index.js:726:26)
    at FSReqCallback.oncomplete (fs.js:166:21)
...