猫鼬护照:“消息”:“用户验证失败:电子邮件:路径`email`是必需的

时间:2019-12-16 19:41:35

标签: express mongoose jwt passport.js

我从字面上审查了8个类似问题的帖子,但无法弄清楚。我正在尝试通过jwt策略使用猫鼬,快递和护照来注册新用户。但我收到"message": "User validation failed: email: Path email is required., lastName: Path lastName is required., firstName: Path firstName is required.",

这是我的模特

    const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let passportLocalMongoose = require('passport-local-mongoose');


let User = new Schema ({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
    },
    characters: {
        type: Array
    }
})

User.plugin(passportLocalMongoose)

module.exports = mongoose.model('User', User);

这是我的路由器:

const bodyParser = require('body-parser');
let passport = require('passport');
let authenticate = require('../authenticate');
let User = require('../models/usersModel');

const usersRouter = express.Router();
usersRouter.use(bodyParser.json());

usersRouter.get('/', (req, res, next) => {
  res.send('users')
});

usersRouter.post('/signup', (req, res, next) => {
  User.register( new User({username: req.body.email }), 
    req.body.password, (err, user) => {
    if (err){
      res.statusCode = 500;
      res.setHeader('Content-Type', 'application/json')
      res.json({err})
    }
    else {
      if(req.body.firstName){
        user.firstName = req.body.firstName
      }
      if(req.body.lastName){
        user.lastName = req.body.lastName
      }
      if(req.body.email){
        user.email= req.body.email
      }
      if(req.body.characters){
        user.characters = req.body.characters
      }
      user.save((err, user) => {
        if (err){
          res.statusCode = 500;
          res.setHeader('Content-Type', 'application/json')
          res.json({err})
          return
        }
        passport.authenticate('local')(req, res, ()=> {
          res.statusCode = 200;
          res.setHeader('Content-Type', 'application/json')
          res.json({ success: true, status: 'Registro exitoso'})
        });
      });
    }
  });
});


module.exports = usersRouter;

and this is the POST i do from postman (setting content type to json)

    {
        "username" : "john@yup.cl",
        "password": "mypass",
        "firstName": "John",
        "lastName": "Smith",
        "email": "john@yup.cl",
        "characters": [1,8,5]
    }

我尝试过,从模型中删除“必需”属性,修改POST,但没有任何效果。请帮忙。

0 个答案:

没有答案