新用户在检查唯一的电子邮件和用户名后不保存到 MongoDB
New user don't save into MongoDB after checking unique email and username
我是网络开发领域的新手。我想创建一个 "user registration & login" 应用程序。但是我未能在注册点将唯一用户信息保存到 MongoDB 中。这是我的代码...
型号:
const mongoose = require('mongoose');
// User Schema
const UserSchema = mongoose.Schema( {
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
password: { type: String, required: true }
} );
const User = module.exports = mongoose.model( 'User', UserSchema );
控制器:
const express = require('express');
const router = express.Router();
const passport = require('passport');
const bcrypt = require('bcryptjs');
const Users = require('../models/userModel');
// post route for Register Process
router.post( '/register', ( req, res ) => {
const name = req.body.name;
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;
// Check email or username already exists in DB
Users.find( { $or: [ {email: email}, {username: username} ] }, ( err, users ) => {
if( err ){ return console.log( err ); }
else{
// If email or username already exists in DB
if( users ){
if( users.length > 0 ){
for( let i = 0; i < users.length; i++ ){
let hasEmail = ( users[i].email == email );
let hasUsername = ( users[i].username == username );
if( hasEmail ){
return console.log( 'Email already registered.' );
}
else if( hasUsername ){
return console.log( 'Username already taken.' );
}
}
}
}
else{
// If email or username is unique / not exists in DB
// then save / create a new user
let newUser = new Users( {
name: name,
email: email,
username: username,
password: password,
confirmPassword: confirmPassword
} );
// Password hashing
bcrypt.genSalt( 10, ( err, salt ) => {
bcrypt.hash( newUser.password, salt, ( err, hash ) => {
if( err ){ return console.log( err ); }
newUser.password = hash;
// New user save in DB / create a new user
newUser.save( ( err ) => {
if( err ){
return console.log( err );
} else{
return console.log( 'You have registered successfully' );
}
} );
} );
} );
}
}
} );
} );
module.exports = router;
所有代码(带服务器)都可以。如果输入 email
或 username
数据库中已经存在任何人,则会收到 error
消息。但是,如果值是唯一的,则 newUser.save()
不会触发(不会保存到数据库中)并且不会在控制台或浏览器中显示任何 error
消息,页面持续加载。在这里,我使用 $or: [ {email: email}, {username: username} ]
通过 email
或 username
.
查找现有用户
我该如何解决这个问题?请帮帮我。
当find()returns[]
没有匹配时,修改你的代码如下:
删除if( users ){
并直接检查长度
if( users.length > 0 ){
//do stuff for existing user
} else {
let newUser = new Users( {
name: name,
email: email,
username: username,
password: password,
confirmPassword: confirmPassword
} );
//save function
}
在你的情况下,你的 if( users)
总是会得到满足,它不会执行 else
将触发 save
的部分
我相信是因为这条线if( users ) {}
。即使您确实提供了唯一的电子邮件和密码,users
也将是一个空数组。在这种情况下:
if (users) {
console.log('Have users'); <- This will get executed although email and username is unique
}
相反,您可以这样做:
if (users && users.length > 0) {
console.log('Have users');
}
此外,您的架构中没有 confirmPassword
字段。
建议:您的代码嵌套过多。您可以像这样简化您的代码。
if( err ) {
console.log( err );
return;
}
// else is not needed
if( users && users.length > 0 ){
// DO YOUR STUFF
return;
}
// else is not needed again
let newUser = new Users( { ... } );
我是网络开发领域的新手。我想创建一个 "user registration & login" 应用程序。但是我未能在注册点将唯一用户信息保存到 MongoDB 中。这是我的代码...
型号:
const mongoose = require('mongoose');
// User Schema
const UserSchema = mongoose.Schema( {
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
password: { type: String, required: true }
} );
const User = module.exports = mongoose.model( 'User', UserSchema );
控制器:
const express = require('express');
const router = express.Router();
const passport = require('passport');
const bcrypt = require('bcryptjs');
const Users = require('../models/userModel');
// post route for Register Process
router.post( '/register', ( req, res ) => {
const name = req.body.name;
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;
// Check email or username already exists in DB
Users.find( { $or: [ {email: email}, {username: username} ] }, ( err, users ) => {
if( err ){ return console.log( err ); }
else{
// If email or username already exists in DB
if( users ){
if( users.length > 0 ){
for( let i = 0; i < users.length; i++ ){
let hasEmail = ( users[i].email == email );
let hasUsername = ( users[i].username == username );
if( hasEmail ){
return console.log( 'Email already registered.' );
}
else if( hasUsername ){
return console.log( 'Username already taken.' );
}
}
}
}
else{
// If email or username is unique / not exists in DB
// then save / create a new user
let newUser = new Users( {
name: name,
email: email,
username: username,
password: password,
confirmPassword: confirmPassword
} );
// Password hashing
bcrypt.genSalt( 10, ( err, salt ) => {
bcrypt.hash( newUser.password, salt, ( err, hash ) => {
if( err ){ return console.log( err ); }
newUser.password = hash;
// New user save in DB / create a new user
newUser.save( ( err ) => {
if( err ){
return console.log( err );
} else{
return console.log( 'You have registered successfully' );
}
} );
} );
} );
}
}
} );
} );
module.exports = router;
所有代码(带服务器)都可以。如果输入 email
或 username
数据库中已经存在任何人,则会收到 error
消息。但是,如果值是唯一的,则 newUser.save()
不会触发(不会保存到数据库中)并且不会在控制台或浏览器中显示任何 error
消息,页面持续加载。在这里,我使用 $or: [ {email: email}, {username: username} ]
通过 email
或 username
.
我该如何解决这个问题?请帮帮我。
当find()returns[]
没有匹配时,修改你的代码如下:
删除if( users ){
并直接检查长度
if( users.length > 0 ){
//do stuff for existing user
} else {
let newUser = new Users( {
name: name,
email: email,
username: username,
password: password,
confirmPassword: confirmPassword
} );
//save function
}
在你的情况下,你的 if( users)
总是会得到满足,它不会执行 else
将触发 save
我相信是因为这条线if( users ) {}
。即使您确实提供了唯一的电子邮件和密码,users
也将是一个空数组。在这种情况下:
if (users) {
console.log('Have users'); <- This will get executed although email and username is unique
}
相反,您可以这样做:
if (users && users.length > 0) {
console.log('Have users');
}
此外,您的架构中没有 confirmPassword
字段。
建议:您的代码嵌套过多。您可以像这样简化您的代码。
if( err ) {
console.log( err );
return;
}
// else is not needed
if( users && users.length > 0 ){
// DO YOUR STUFF
return;
}
// else is not needed again
let newUser = new Users( { ... } );