ValidationError: сбой проверки пользователей: tname: требуется путь `tname`

Я пытаюсь сделать проект, который требует от меня использовать модуль bcryptjs. Проект работал до сих пор, и сервер все правильно публиковал; однако, когда я добираюсь до этого шага, я начинаю получать эту ошибку:

{ ValidationError: users validation failed: tname: Path `tname` is required.
    at ValidationError.inspect (C:\Users\Ace\vidjot\node_modules\mongoose\lib\error\validation.js:56:24)
    at formatValue (util.js:408:38)
    at inspect (util.js:298:10)
    at format (util.js:165:12)
    at Console.log (console.js:131:21)
    at newUser.save.then.catch.err (C:\Users\Ace\vidjot\routes\users.js:64:27)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
  errors:
   { tname:
      { ValidatorError: Path `tname` is required.
    at new ValidatorError (C:\Users\Ace\vidjot\node_modules\mongoose\lib\error\validator.js:25:11)
    at validate (C:\Users\Ace\vidjot\node_modules\mongoose\lib\schematype.js:782:13)
    at C:\Users\Ace\vidjot\node_modules\mongoose\lib\schematype.js:831:11
    at Array.forEach (<anonymous>)
    at SchemaString.SchemaType.doValidate (C:\Users\Ace\vidjot\node_modules\mongoose\lib\schematype.js:791:19)
    at C:\Users\Ace\vidjot\node_modules\mongoose\lib\document.js:1570:9
    at process._tickCallback (internal/process/next_tick.js:150:11)
        message: 'Path `tname` is required.',
        name: 'ValidatorError',
        properties: [Object],
        kind: 'required',
        path: 'tname',
        value: undefined,
        reason: undefined,
        '$isValidatorError': true } },
  _message: 'users validation failed',
  name: 'ValidationError' }

Вот мой код:

const express = require('express');

const mongoose = require('mongoose');

const bcrypt = require('bcryptjs');

//const passport = require('passport');

const router = express.Router();


// Load User Model

требовать ('../ модели / User');

const User = mongoose.model ('users');

// Авторизация пользователя

router.get('/login', (req, res) => {
  res.render('users/login');
});


// User Register Route
router.get('/register', (req, res) => {
  res.render('users/register');
});

// Register Form POST
router.post('/register', (req, res) => {
  let errors = [];

  if(req.body.password != req.body.password2){
    errors.push({text:'Passwords do not match'});
  }

  if(req.body.password.length < 4){
    errors.push({text:'Password must be at least 4 characters'});
  }

  if(errors.length > 0){
    res.render('users/register', {
      errors: errors,
      name: req.body.name,
      email: req.body.email,
      password: req.body.password,
      password2: req.body.password2
    });
  } else {
    User.findOne({email: req.body.email})
      .then(user => {
        if(user){
          req.flash('error_msg', 'Email already regsitered');
          res.redirect('/users/register');
        } else {
          const newUser = new User({
            name: req.body.name,
            email: req.body.email,
            password: req.body.password
          });

      bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
          if(err) throw err;
          newUser.password = hash;
          newUser.save()
            .then(user => {
              req.flash('succes_msg', 'You are now registered and can log in');
              res.redirect('/users/login');
            })
            .catch(err => {
              console.log(err);
              return;
            });
        });
      });
    }
  });

}});

module.exports = router;

0 ответов

Другие вопросы по тегам