简体   繁体   中英

Node.js async.each - “callback was already called”

I am a node.js noob and am trying to do some file processing. I'm using async to process an array of files but the callback function is never called. I believe this is due to calling the next() function twice but I can't see where I'm doing this. If I comment out the last "return next()" I finish with no errors but the final callback doesn't execute. If I uncomment out this line I get the error message "callback was already called". Any help would be greatly appreciated. Here is the code:

/*jslint node: true */
"use strict";

var fs = require('fs'),
    dive = require('dive'),
    subdirs = require('subdirs'),
    async = require('async'),
    currentYear = new Date().getFullYear(),
    cpFile = __dirname + "/" + "header.txt",
    noCopy = __dirname + "/" + "noCopyright.txt",
    currentHeads = __dirname + "/" + "currentHeaders.txt",
    reYear = /\s(\d{4})[-\s]/i, // matches first 4 digit year
    reComment = /(\/\*(?:(?!\*\/).|[\n\r])*\*\/)/, // matches first multi-line comment
    allHeaders = {},
    stringObj,
    year,
    top;

function needsHeader (file) {
    if ((file.match(/.*\.js$/) || file.match(/.*\.less$/) || file.match(/.*\.groovy$/) || file.match(/.*\.java$/) || file.match(/.*\.template$/) || file.match(/.*\.html$/))) {
        fs.appendFile(noCopy, file + "\n", function (err) {
            if (err) {
                return console.log(err);
            }
        });
    }
}

fs.readFile(cpFile, 'utf8', function (err, copyRight) {
    if (err) {
        return console.log(err);
    }
    subdirs(__dirname, 4, function (err, dirs) {
        if (err) {
            return console.log(err);
        }
        async.each(dirs, function (dir, next) {
            if (! dir.match(/.*\/src$/)) {
                return next();
            } else {
                dive(dir, {all: false}, function (err, file) {
                    if (err) {
                        return next(err);
                    } else {
                        fs.readFile(file, 'utf8', function (err, data) {
                            if (err) {
                                return next(err);
                            } else {
                                if (data.match(reComment) && (file.match(/.*\.js$/) || file.match(/.*\.less$/) || file.match(/.*\.groovy$/) || file.match(/.*\.java$/) || file.match(/.*\.template$/))) {
                                    top = data.match(reComment)[0];
                                    if (top.match(reYear)) {
                                        year = top.match(reYear)[1];
                                        if (allHeaders[year]) {
                                            allHeaders[year].push(file);
                                        } else {
                                            allHeaders[year] = [file];
                                        }
                                    } else {
                                        needsHeader(file);
                                    }
                                } else {
                                    needsHeader(file);
                                }
                                return next();
                            }
                        });
                    }
                });
            }
        }, function (err) {
            if (err) {
                console.log(err);
            }
            stringObj = JSON.stringify(allHeaders, null, 4);
            fs.writeFile(currentHeads, stringObj, function (err) {
                if (err) {
                    return console.log(err);
                }
            });

        });
    });
});

It expects you to call next() for each directory, and you are calling it for each file found in the directory. So as soon as some directory contains 2 or more files, you get the error.

To fix it, try call next() on dive complete. See the dive documentation :

complete [optional] may define a second callback, that is called, when all files have been processed. It takes no arguments.

dive(dir, {all: false}, function (err, file) {
  if (err) {
    return next(err);
  } else {
      // your file handling code here
  }
}, function complete() {
  next();
});

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