shubhamagiwal92
shubhamagiwal92

Reputation: 1432

Unable to understand ASYNC series in ASYNC npm package

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:

  1. I check whether the email Id exist or not. If it exists, then I give an error saying that the email id exists.
  2. If the Email id doesn't exists, then I hash the given password and I store the email,firstName,lastName and hashed password in mongoDB.

Can Somebody tell me as to where am I making a mistake?

Upvotes: 1

Views: 101

Answers (1)

JohnnyHK
JohnnyHK

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

Related Questions