user3569641
user3569641

Reputation: 922

Node js callback

var signupHelper = require('./util/signuphelper');

router.post('/signup', signupHelper.checkEmpty, signupHelper.test, function(req,res){   });

I'm new to node.js (don't know if it is called callback.). My problem is, when signupHelper.checkEmpty has no errors it doesn't return anything, so it cannot proceed to .test. How can i fix this? Cos if there's no error, I want to check the data if it doesn't exist in the db so I need to call signupHelper.test to check.

In my checkEmpty

exports.checkEmpty = function(req,res){ 
------- some code here -------  
if(errors.length > 0){
        req.flash('errors', errors);
        res.redirect('/signup');
    }
}

exports.test = function(req,res) { some code....}

Upvotes: 0

Views: 85

Answers (1)

Tom V.
Tom V.

Reputation: 323

You need to call the next parameter in the router callback function, look at the next(); that comes from the router callback.

exports.checkEmpty = function(req,res,next){ 
------- some code here -------  
if(errors.length > 0){
        req.flash('errors', errors);
        res.redirect('/signup');
    }else{
        next(); // This will pass the route to the next callback in chain.
    }
}

exports.test = function(req,res,next) { some code....}

Upvotes: 4

Related Questions