猫鼬不会保存用户

时间:2019-12-23 22:25:47

标签: javascript node.js mongodb express mongoose

我在这里做错了什么?我的数据库已连接,但由于某种原因猫鼬无法保存用户。我几乎已经尝试了一切,但我不知道。别人可以帮我忙吗?

var express = require('express');
const Bcrypt = require("bcryptjs");
var router = express.Router();
const User = require('../models/User');

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

//Sign up page GET
router.get('/register', function(req, res, next) {
  res.render('signup');
});

//Sign up page POST
router.post('/register', async (req, res) => {
  try{
   req.body.password = Bcrypt.hashSync(req.body.password, 10);

  const user = new User({
    firstName: req.body.firstname,
    lastName: req.body.lastname, 
    description: req.body.description,
    email: req.body.email,
    password: req.body.password
  })

  const result = await user.save(); 

  res.send('User saved'); 
}
catch (error){
  res.status(500).send(error);
}
});

//Login page GET
router.get('/login', function(req, res, next) {
  res.render('login');
});

module.exports = router;

0 个答案:

没有答案