[英]Node.js async.each - “callback was already called”
I am a node.js noob and am trying to do some file processing. 我是一个node.js新手,正在尝试做一些文件处理。 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. 我相信这是由于两次调用next()函数造成的,但是我看不到我在哪里执行此操作。 If I comment out the last "return next()" I finish with no errors but the final callback doesn't execute. 如果我注释掉最后一个“ return next()”,则不会出现任何错误,但最终的回调不会执行。 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. 它期望您为每个目录调用next()
,并且为目录中找到的每个文件调用它。 So as soon as some directory contains 2 or more files, you get the error. 因此,只要某个目录包含2个或更多文件,您就会收到错误消息。
To fix it, try call next()
on dive complete. 要解决此问题,请尝试在潜水完成时调用next()
。 See the dive documentation : 请参阅潜水文档 :
complete [optional] may define a second callback, that is called, when all files have been processed. complete [可选]可以定义所有文件都处理完后的第二个回调。 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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.