Search code examples
node.jsexpressejsexpress-validator

Why am I returing an [Object Promise] instead of a callback


I am trying to set up a registration page that uses express-validator. However I am receiving the following error whenever I try and run my app. I'm guessing it has something to do with the Validator portion since I recently transitioned to a newer version of the middleware (6.2). Before that it was pretty out dated but working fine.

   Error: Route.post() requires a callback function but got a [object Promise] 

and here is the POST

// Register - POST
router.post('/register', [
    check('userphonenumber', 'a user phone number is required').not().isEmpty(),
    check('fname', 'First name field is required').not().isEmpty(),
    check('lname', 'Last name field is required').not().isEmpty(),
    check('email', 'Email field is required').not().isEmpty(),
    check('email', 'Please use a valid email address').not().isEmpty()(),
    check('username', 'Username field is required').not().isEmpty(),
    check('password', 'Password field is required').not().isEmpty(),
    check('passwordConfirm', 'Passwords do not match').equals(body.password),
    check('zipCode', 'Zip Code field is required').not().isEmpty(),
    ], function(req, res) {
    // Get Form Values
    var fname           = req.body.fname;
    var lname           = req.body.lname;
    var email           = req.body.email;
    var username        = req.body.username;
    var phonenumber     = req.body.phonenumber;
    var password        = req.body.password;
    var passwordConfirm = req.body.passwordConfirm;
    var zipCode             = req.body.zipCode;


    // Check for errors
    const result = validationResult(req);
    var errors = result.errors;

    console.log(errors);

          if (!result.isEmpty()) {
            res.render('techregister', {
            errors: errors,
            fname: fname,
            lname: lname,
            email: email,
            username:username,
            password: password,
            passwordConfirm: passwordConfirm,
            zip : zipCode
            });
          }
     else {
        var newTech = {
            fname: fname,
            lname: lname,
            phonenumber: phonenumber,
            email: email,
            username:username,
            password: password,
            zipCode: zipCode,   
        }

        bcrypt.genSalt(10, function(err, salt){
            bcrypt.hash(newTech.password, salt, function(err, hash){
                newTech.password = hash;
                        techdb.users.insert(newTech, function(err, doc){
                            if(err){
                                res.send(err);
                            } else {
                                console.log('User Added...');


                                //Success Message
                                req.flash('success', 'You are registered please confirm with your email before you log in');

                                // Redirect after register
                                res.location('/admins/login');
                                res.redirect('/admins/login');
                            } 
                        });
                    });

                });
            }
        });

Solution

  • The second email check has extra parenthesis ()

    router.post('/register', [
    check('userphonenumber', 'a user phone number is required').not().isEmpty(),
    check('fname', 'First name field is required').not().isEmpty(),
    check('lname', 'Last name field is required').not().isEmpty(),
    check('email', 'Email field is required').not().isEmpty(),
    check('email', 'Please use a valid email address').not().isEmpty(),
    ], function(req, res)
    {
    //...
    });