简体   繁体   中英

async in Node Express

I'm kind of new to async function and kind of confused how to pass callbacks in async.each for each item to the next level (middleware)

Logic: I want to go through all items and if item is "type2" I need to go through emailExists to check if the email exists or not! Afte processing all items we go to the next middleware only if there was no item with type2 or if there is any email exists! here I have mentioned my sudocode; I really appreciate it if you help me with async function which I'm pretty new to it and please let me know what I'm doing wrong:

Please note in the second block always err and data are undefined?! Why is that? Even though it is going to emailExistence.check!

Please let me know if you need any more clarification! Thanks :-)

    async.each(Object.keys(items), function (item, callback) {


        if (!req.body[item] && items[item].type) {
            // assiging the default values based on the default type                    
            switch (items[item].type1) {
                case 'name1':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name2':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name3':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name4':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                default:
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
            }
        } else if (items[item].type2) {
            emailExistence.check(req.body[item], function (err, exists) {

                if (err) {
                    callback(err);
                } else {                    
                    callback(null, exists);
                }
            });

        } else {
            callback();
        }
    },
            function (err, data) {
                // Here always err and data are undefined?! Why is that? Even though it is going to emailExistence.check!
                if (err) {
                    res.json(400, err);
                } else {
                    if (data) {
                        next();
                    } else {
                        res.json(400, {"data": "email not exist"});
                    }else{
                         next();
                    }
                }
        }
)

UPDATED(in response to Aaron):

Aaron you are right, async.each returns just one parametere(err); but the map verion is not going to work if we have it inside for loop! Could you please help me; is this the version you are prposing with map:

function middleware(req, res, next) {
    for (var item in items) {


        if (!req.body[item] && items[item].type) {
            // assiging the default values based on the default type                    
            switch (items[item].type1) {
                case 'name1':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name2':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name3':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                case 'name4':
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
                default:
                    //.
                    //.No callback functions here
                    //.
                    callback();
                    break;
            }
        } else if (items[item].type2) {
            var emails = [];
            emails.push(req.body[field]);
            // In my case here there is just  one email (array of one item)
            async.map(emails, checkEmail, function (err, result) {
                if (!results) {
                    res.json(400, {"msg": "email does not exist"});
                }

            });

        }

    }
    next();


    function checkEmail(email, callback) {

        emailExistence.check(email, function (err, exists) {

            if (err) {

                callback(err);
            } else {

                callback(null, exists);
            }
        });
    }
}

async.each is only useful for side-effects. As the documentation says, the callback passed to the iterator only expects on argument, and the final callback only gets one argument - err . You probably want async.map , which expects err and transformed in the callback passed to the iterator and passes err and results to the final callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM