Node.js check if user already exists

node.js check if email already exists
check email and username already exist node js sequelize
node js check if user exists mysql
node js mongodb check if document exists
check if user already exists without submitting form
email already exist nodejs and mysql
how to check email exist or not in node js
node js check if record exists

Hello guys I am trying to check if a user already exists in the database, I have managed to stop creating a user if one already exists with the same name, however I do not seem to get the error message displayed. I am not too sure why my error is not being handled correctly. Here is my code:

 // Register User
    router.post('/register', function(req, res){
        var name = req.body.name;
        var email = req.body.email;
        var username = req.body.username;
        var password = req.body.password;
        var password2 = req.body.password2;

    //Validation
    req.checkBody('name', 'Name is required').notEmpty();
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();
    req.checkBody('password2', 'Passwords do not match').equals(req.body.password);

    var errors = req.validationErrors();

    if(errors){
        res.render('register',{
            errors:errors
        });
    }else{var newUser = new User({
        name: name,
        email:email,
        username: username,
        password: password
    });

    User.getUserByUsername(username, function(err, user){
        if(err) throw err;
        if(user){
            return new Error('User already exists!');
        }else{
            User.createUser(newUser, function(err, user){
                if(err) throw err;
                console.log(user);
            });
        }
    });


    req.flash('success_msg', 'You are registered and can now login');

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

This will work exactly as you wish.I had the same issue trying to make it work and I assure you it does. Cheers!

User.getUserByUsername(username, function(err, user){ //must check if user exists
  if(err) throw err;
  if(user){
    console.log('existing...'); //may be deleted
    req.flash('error_msg', 'This username already exists');
    res.redirect('/users/register');
  } else {
    User.createUser(newUser, function(err, user){
    if(err) throw err;
    console.log(user);//may be deleted
    });

  req.flash('success_msg', 'You registered successfuly and you can now login');
  res.redirect('/users/login');
  }
})

Node.js check if user already exists, Solved it with this if anyone else is trying to do the same thing: in app.js app.get('/ usercheck', function(req, res) { User.findOne({username:� You need a database where you store all your users information, including their username. Then you query that database for a user that has the username you're checking. If the query returns a user, then the name is taken otherwise it isn't. Basically you need to read about databases.

Node.js - Check if user exists, Hello guys I am trying to check if a user already exists in the database, I have managed to stop creating a user if one already exists with the� The Node.js fs native module provides several useful methods that you can use to work with directories. The simplest way to check if a certain directory exists in Node.js is by using the fs.existsSync() method. The existsSync() method asynchronously checks for the existence of the given directory. Here is an example:

var candidateName = username;
var queryforUsername = {};
queryforUsername['username'] = candidateName;
User.find(queryforUsername, function(err, item){
    if(err) throw (err);
    if(item.length > 0){
        req.flash('error_msg', 'User with username '+username+' already exists!');
        res.redirect('register');
    }else if(item.length == 0){
        User.createUser(newUser, function(err, user){
            if(err) throw err;
            console.log(user);
            req.flash('success_msg', 'You are registered and can now login');
            res.redirect('/users/login');
        });
    }
});

Error for checking user already exist in node js using couchbase , js. I'm trying to prevent registration with a previously registered email. I tried to create a custom validation in mongoose schema. The following Stored Procedure will be used to check whether the supplied Username exists in the database or not. It will return TRUE if the Username does not exists in database i.e. it is available and it will return FALSE if the Username exists in the database i.e. it is already in use.

If you only want to check if user with given username exist or not, then I have used this simple trick.

What I am doing is while registering, if some error occurs then I am checking if this error is UserExistError(which is related to mongodb, and you can see it when it occurs in terminal)

If err is equal to UserExistError then I am rendering an alert which is in separate file.

register(new User({username:req.body.username}),req.body.password,function(err,user){
    if(err)
    {   var chk=1;
        console.log(err);
        if(err=='UserExistsError');
        console.log("error is here");
        return res.render("register",{chk:chk});

    }

    passport.authenticate("local")(req,res,function(){
        res.redirect("/dashboard");
    })    

});

How to check if user with email already exists? - node.js - html, I am trying to check if an email and an username in a database do already exist. Since I am using mongoose and express.js, I only checked if the username in a� Next we make sure Express is initialized. The router.post() function does all the heavy lifting here. First the http post request gets validated, then we check to see if the user already exists in the database, and finally we create a new user if they do not exist in the database and also if they pass all validation requirements.

check if user name and email in database using expressJS, In this tutorial, we'll do it in a more efficient and fun way (using JQuery and Ajax,) without If a user with the provided username already exists in the database,� Learn how to check if a file exists in the local file system using Node.js FS module. To offer you a better browsing experience, we use cookies. By using this site, you agree to our Cookies Policy .

Check if user already exists without submitting form, First the http post request gets validated, then we check to see if the user already exists in the database, and finally we create a new user if they do not exist in� Node.js Redirect URL inside Node Request I am trying to redirect to another url immediately after my 1st url request is completedThe reason for that is because in order to open 2nd url, it requires the 1st url session cookie

Node.js MongoDB User Registration – Vegibit, 11 votes, 32 comments. I am making a project with node js and mongo db on the backend and html css on the front end. I wanna check if the username … The user visits the register page, enters their username, email and password. When the cursor leaves the username field (an event referred to in JavaScript as onblur, a username check is done in the background through an ajax call to the server without the user even clicking the submit button. If a user with the provided username already exists in the database, a validation message with appropriate styling is displayed right away.

Comments
  • Thanks for the reply. Here is what I did and it works if anyone is interested.
  • if(err) throw (err); This error wouldn't get handled by Express router. Please see my answer for details.