最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use a custom express-validator? - Stack Overflow

programmeradmin1浏览0评论

I'm developing a register form and I'm using express-validator to validate fields but I'm having troubles in validate if email already exists.

I did a select to check if the inserted email already exists but it returns me always that email exists.

I tried other ways but didn't work.

This is my post request:

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

    // Validation
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
  req.checkBody('email', 'Email already exists').custom(value => {
    return User.findUserByEmail(value).then(function(user) {
      throw new Error('this email is already in use');
    })
  });

    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){
    console.log(errors);
        res.render('register',{
            errors:errors
        });
    } else {
    console.log("passed");

        var newUser = {
            email:email,
            username: username,
            password: password
        };


        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('/login');
    }
});

And my function findUserByEmail:

module.exports.findUserByEmail = function(email, callback){
  var connection = db.setDB('DB1');
  connection.then(result => {
    var request = new mssql.Request(conn);
    console.log("SELECT * FROM u_users WHERE email = '"+email+"'");
    var exists = request.query("SELECT * FROM u_users WHERE email = '"+email+"'");

    Promise.all([exists]).then(function(results)
    {

      console.log(results);
      if(results[0].length == 0)
      {
        console.log("1");
        return false;
      }
      console.log("2");
      return true;
    }).catch(function(err)
    {
      console.log("Error verifying email...");
      console.log(err);
    });
  });
}

If I do this way it returns me an error Cannot read property 'then' of undefined.

What am I doing wrong?

I'm developing a register form and I'm using express-validator to validate fields but I'm having troubles in validate if email already exists.

I did a select to check if the inserted email already exists but it returns me always that email exists.

I tried other ways but didn't work.

This is my post request:

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

    // Validation
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
  req.checkBody('email', 'Email already exists').custom(value => {
    return User.findUserByEmail(value).then(function(user) {
      throw new Error('this email is already in use');
    })
  });

    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){
    console.log(errors);
        res.render('register',{
            errors:errors
        });
    } else {
    console.log("passed");

        var newUser = {
            email:email,
            username: username,
            password: password
        };


        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('/login');
    }
});

And my function findUserByEmail:

module.exports.findUserByEmail = function(email, callback){
  var connection = db.setDB('DB1');
  connection.then(result => {
    var request = new mssql.Request(conn);
    console.log("SELECT * FROM u_users WHERE email = '"+email+"'");
    var exists = request.query("SELECT * FROM u_users WHERE email = '"+email+"'");

    Promise.all([exists]).then(function(results)
    {

      console.log(results);
      if(results[0].length == 0)
      {
        console.log("1");
        return false;
      }
      console.log("2");
      return true;
    }).catch(function(err)
    {
      console.log("Error verifying email...");
      console.log(err);
    });
  });
}

If I do this way it returns me an error Cannot read property 'then' of undefined.

What am I doing wrong?

Share Improve this question edited Feb 20, 2018 at 11:34 Marco 2,09219 silver badges29 bronze badges asked Feb 19, 2018 at 23:35 user3242861user3242861 1,92912 gold badges53 silver badges97 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 0

Regarding the error Cannot read property 'then' of undefined., this happens because you're not returning the promise from your findUserByEmail function.

In addition to this, your custom validator will always fail if you keep it as is.

Your findUserByEmail is only returning true/false, what means "resolve this promise with this value".
You might want to change it to return the found user (and also make its name suggest what it really does!):

module.exports.findUserByEmail = function(email, callback){

    var request = new mssql.Request(conn);
    console.log("SELECT * FROM u_users WHERE email = '"+email+"'");
    var query = request.query("SELECT * FROM u_users WHERE email = '"+email+"'");

    return query.then(function(results) {
      return results[0];
    }).catch(function(err) {
      console.log("Error verifying email...");
      console.log(err);
      throw err;
    });
}

Now you need to make your custom validator check if any user was returned from that function:

req.checkBody('email', 'Email already exists').custom(value => {
    return User.findUserByEmail(value).then(function(user) {
        if (user) {
            throw new Error('this email is already in use');
        }
    })
});

And you're done!

发布评论

评论列表(0)

  1. 暂无评论