fs.readFileexception

试图运行一个脚本来asynchronous打开一堆文件并读取它们的内容。 我得到一个错误,其中fs.readFile的callback进来没有数据,但该文件是在那里,并没有任何其他打开。 完全困惑。

错误是:

错误:确定,打开“D:\ Workspace \ fasttrack \ public \ cloudmade \ images \ 998 \ 256 \ 6 \ 31 \ 61.png”

更多信息:

该程序运行一个循环,其中有一堆对象,看起来像这样:

 newObject = { filePath:filePath, scanPixels:function(parent){ .... } } 

循环调用每个对象的scanPixels函数,然后在fs.readFile上执行parent.filePath

这是for循环

 for(var index=0;index<objects.length;index++){ objects[index].scanPixels(objects[index]); } 

scanPixels函数本质上是这样的:

 scanPixels:function(parent){ png_js.decode(parent.filePath, function(pixels){ ...more stuff 

并在png_js文件中:

 PNG.decode = function(path, fn) { return fs.readFile(path, function(err, file) { var png; png = new PNG(file); return png.decode(function(pixels) { return fn(pixels); }); }); }; 

问题是fs.readFile没有返回值。 你会想为此fs.readFileSync

 var buffer1 = fs.fileReadSync('./hello1.txt'); var buffer2; fs.fileRead('./hello2.txt', function (err, file) { buffer = file; });