Reputation: 1432
Hi I wanted to remove the ASYNC OF HELL from my API. I came across an npm package namely async which gives the option called async series. So I tried it in my API where I am making a normal Register form which takes the email, firstName, LastName and password from the user and inserted breakpoints using node inspector to see its working. I posted my data using DHC. My API wasn't able to give me the desired output and it didn't post an error also. My code is as follows
function email_verification(callback) {
User.find(email, function (err, data) {
if (err) {
callback(err);
}
});
}
function hashing_password(callback) {
bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
if (err) {
callback(err);
}
bcrypt.hash(password, salt, function (err, hash) {
if (err) {
callback(err);
}
});
});
}
function saving_user(callback) {
password = hash;
delete param.password;
param.password = password;
var user_details = db.User(param);
user_details.save(param, function (err, data) {
if (err) {
callback(err);
} else {
delete data._doc.password;
return res.json({
success: true,
user_details: data
});
}
});
}
async.series([
email_verification,
hashing_password,
saving_user
], function (err) {
if (err.name != null) {
if (err.name === "ValidationError") {
return res.json({
success: false,
exception: "ERROR.USER.ALREADY.EXISTS"
});
} else if (err) {
return res.json({
success: false,
exception: err
});
} else {
return res.json({
success: true,
details: err
});
}
}
});
My API flow is as follows:
Can Somebody tell me as to where am I making a mistake?
Upvotes: 1
Views: 101
Reputation: 312075
You need to be calling callback
in your functions in the success case as well (not just the error case).
For example:
function email_verification(callback) {
User.find(email, function (err, data) {
if (err) {
callback(err);
} else {
callback(err, data);
}
});
}
Which you can simplify to:
function email_verification(callback) {
User.find(email, callback);
}
Upvotes: 2