Giter Site home page Giter Site logo

Comments (3)

Saspect-IO avatar Saspect-IO commented on May 27, 2024

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.

https://stackoverflow.com/questions/28649593/how-to-ensure-a-unique-email-username-will-enter-into-mongodb-using-mongoose

from node_passport_login.

chia7 avatar chia7 commented on May 27, 2024

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.

Saspect-IO avatar Saspect-IO commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.