This is my first try at javascript and I am trying to validate if the email is already present in the database or not. But before my find query the "if" statement is working. If you have a better way to do this, feel free to share. Thank you for your time.
router.post('/register',[
check('name').not().isEmpty(),
check('dob').not().isEmpty(),
check('gender').not().isEmpty(),
check('email').not().isEmpty(),
check('email').isEmail(),
check('username').not().isEmpty(),
check('password').not().isEmpty(),
check('password2').not().isEmpty()],function(req,res){
const name = req.body.name;
const dob = req.body.dob;
const gender = req.body.gender;
const email = req.body.email;
const username = req.body.username;
const password = req.body.password;
var temp;
var query = {email: email}
User.findOne(query,function(err,user){
if(err)
{
message: err.message;
return;
}
if(user)
{
temp=user.email;
}
});
//THIS IS CHECKING temp BEFORE user CHANGES THE VALUE OF temp
if(temp !== null)
{
req.flash('danger',"Email already exists.");
res.redirect('/users/register');
return;
}