Comments (3)
That's because there are no params set for username. You can set it to unique in the model schema and drop duplicate. See link below. You can also setup a search function to check the database by username and if input is in database then alert user to enter new username.
from node_passport_login.
Can I use customize express-validator to do that? I've try a few ways but they do not work out. The User.findOne function would not return instantly, cause the registration success.
from node_passport_login.
You need to use a promise, Try this code below, modified from this link :https://stackoverflow.com/questions/35008433/i-am-using-node-js-promise-for-validating-either-username-exist-in-db-or-not
function findUser() {
return User.findOne({"username": req.body.username}).then(function(user) {
if (user.username) {
console.log(user.username);
req.flash('success_msg', 'Username Already Exist, Please Enter a different username');
res.redirect('/users/register');
throw new Error('Username already exists!');
}
// user doesn't exist, all is good in your case
}, function(err) {
// handle mongoose errors here if needed
// rethrow an error so the caller knows about it
throw new Error('Some Mongoose error happened!');
// or throw err; if you want the caller to know exactly what happened
});
}
Then you call the find user function here.
findUser().then(function() {
// user doesn't exist, do your stuff
User.createUser(newUser, function(err, user){
f(err){
}
});
req.flash('success_msg', 'You are registered and can now login');
res.redirect('/users/login');
}).catch(function(err) {
// here, you'll have Mongoose errors or 'User already exists!' error
console.log(err.message);
});
Really hope this helps
from node_passport_login.
Related Issues (20)
- UnhandledPromiseRejectionWarning: MongooseError: Operation `users.findOne()` buffering timed out after 10000ms HOT 4
- Error on opening Register Page HOT 2
- Error On Opening Login Page HOT 1
- Problem with passport middle authentication HOT 2
- title for test issue HOT 1
- Test 2
- test 3
- test
- Register >> if email already exists
- SyntaxError: Unexpected token '/'
- email Match
- Please help ... Bootstrap is not working
- messages.ejs showing errors (Screenshot Attached.) HOT 2
- Include statement in ejs file not correct HOT 1
- ReferenceError: newUser is not defined HOT 1
- How to deploy in IBM cloud? HOT 1
- Could not find the include file "./partials/messages" HOT 2
- _mongoose.Error.OverwriteModelError(name); HOT 2
- Error: No default engine was specified and no extension was provided. at new View (D:\Node_Passport\node_modules\express\lib\view.js:61: HOT 1
- MongoDB Compass Local URI not connecting HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node_passport_login.