javascript - 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;
解决方案
你的代码说
const newUser = new user({
但在这种情况下user
是来自你的承诺的变量then
。你想要User
哪个是你的 mongo 类。
推荐阅读
- r - R如何在geom_tile中叠加?
- r - 在多列上使用 tidyr 的 pivot_wider 的问题
- c - Linux C - 将 `iphdr->daddr` 转换为等效字符串 - 不再起作用?
- php - 如何在 php 中通过动态输入 Html 将数据添加到数组中
- linux - 如何在 Linux 命令行中使用 wget 将网页内容下载到 .txt 文件中?
- regex - 如何仅获取正则表达式 Grok 过滤器的第一个匹配项
- jenkins - 在 Jenkins 管道作业中使用包含区域进行 SCM 轮询
- ios - 位置:粘滞闪烁 Safari iOS 仅
- google-cloud-platform - 如何将气流 DataFlowPythonOperator 用于光束管道?
- html - How to make div into center using html or css