首页 > 解决方案 > node js中的登录系统

问题描述

我是 node.js 的新手,我正在构建一个用户登录系统,但是我收到了错误,因为我在注册新用户时遇到了这个错误

MongoDB 连接

(node:12592) UnhandledPromiseRejectionWarning: TypeError: user is not a constructor
    at User.findOne.then.user (C:\Users\AKASH TOMAR\Desktop\miniproject\routes\users.js:59:35)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:12592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12592) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:12592) UnhandledPromiseRejectionWarning: TypeError: user is not a constructor
    at User.findOne.then.user (C:\Users\AKASH TOMAR\Desktop\miniproject\routes\users.js:59:35)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:12592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

我没有找到我做错的地方请帮助我,因为明天我必须在我的大学展示,我已经评论了我的代码也为了正确的地位。

const express = require('express');

const router = express.Router();
const bcrypt = require('bcryptjs');
//User model
const User = require('../models/User');


//Login page
router.get('/login',(req,res)=>res.render('login'));

//register page
router.get('/register',(req,res)=>res.render('register'));

//Register Hnadle
router.post('/register',(req,res)=>{
    const {name,email,password,password2 } = req.body;
    let errors = [];

    //check required fields
    if(!name || !email || !password || !password2){
        errors.push({msg: 'Please fill in all fields'});
    }

    //check passwords match
    if(password !== password2){
        errors.push({msg: 'Passwords do not match'});
    }
    //check password length
    if(password.length < 6){
        errors.push({msg:'Password should be at least 6 chracters'});

    }
    if(errors.length>0){
        res.render('register',{
            errors,
            name,
            email,
            password,
            password2

        });
    }else{
        //validation passed
        User.findOne({email: email})
          .then(user => {
              if(user){
                  //user exists
                  errors.push({msg: 'Email is already registered'});
                  res.render('register',{
                    errors,
                    name,
                    email,
                    password,
                    password2

                });
              }else {
                  const newUser = new user({
                      name,
                      email,
                      password
                  }); 
                  //hash 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(

                  'success_msg',

                  'You are now registered and can log in'

                );

                res.redirect('/users/login');

              })

              .catch(err => console.log(err));

          });

        });

      }

    });

  }

});
module.exports = router;

标签: javascriptnode.jsmongodbexpress

解决方案


你的代码说

   const newUser = new user({

但在这种情况下user是来自你的承诺的变量then。你想要User哪个是你的 mongo 类。


推荐阅读