简体   繁体   中英

Callback was already called in async.foreachSeries

In my function I am getting error as Callback was already called. I am not sure why this issue is there as I am not calling callback inside async.forEachSeries.

var invalid = true;
async.forEachSeries(book.titles, function (title, titleCallback) {

            authorizeContext(permission,contextSet, book.bookId, function (error, validContext) {
                if (error) {

                    isvalid = false;
                    titleCallback("User is not authorized");
                } else if (validContext) {
                    logger.info("user is authorized")
                    isvalid = true;
                    titleCallback(null);
                } else {
                    logger.info("c");
                    isvalid = false;
                    titleCallback("User is not authorized");
                }
            });
        }, function (err) {
            if (err && isvalid) {
                logger.info("inside error"+JSON.stringify(err));
                callback(err, null)
            } else {
                logger.info("inside result"+isvalid);
                callback(null, isvalid)
            }
        });

Above method is correct and no issue in callbacks. Issue was in the method called authorizeContext(). This method was doing async call under a foreach loop, so replacing the foreach loop with async.foreach worked for me.

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