I am new to node js and I am stuck with nested callback.
I want callback to be executed first and then only line after callback needs to be executed.
Here is my sample code :
signup: function (req, res) {
var user = UserModel.checkIfDataExists({email: req.body.email, isAccountVerified: 1}, function (user) {
//console.log(user);
var mobileUser = UserModel.checkIfDataExists({mobileNumber: req.body.mobileNumber, 'country.code': req.body.countryCode, isAccountVerified: 1}, function (mobileUser) {
console.log(mobileUser);
});
});
res.send('Success');
}
Here is helper function :
checkIfDataExists: function (value, callback) {
User.findOne(value)
.exec(function (err, result) {
if (err) {
log('Error in Login With Mobile API : ', err.message);
callback(false);
} else {
callback(result);
}
});
}
Upon executiong current code, I am getting null in console as line is executed(commented console.log(user)) and after that callback is returning while if I print console for outer callback then it is able to print that in log. Inner callback is not working.