Trying to run a script that opens a bunch of files asynchronously and reads their content. I am getting an error where fs.readFile's callback comes in with no data, yet the file is there, and is not currently being opened by anything else. Totally confused.
The error is:
Error: OK, open 'D:\Workspace\fasttrack\public\cloudmade\images\998\256\6\31\61.png'
More info:
The program runs through a loop that has a bunch of objects in it that look like this:
newObject = {
filePath:filePath,
scanPixels:function(parent){
....
}
}
The loop calls each object's scanPixels function, which then does an fs.readFile on the parent.filePath
Here is the for loop
for(var index=0;index<objects.length;index++){
objects[index].scanPixels(objects[index]);
}
The scanPixels function is essentially this:
scanPixels:function(parent){
png_js.decode(parent.filePath, function(pixels){
...more stuff
And in the png_js file:
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);
});
});
};
the problem is that fs.readFile does not return a value. You would want fs.readFileSync for that
var buffer1 = fs.fileReadSync('./hello1.txt');
var buffer2;
fs.fileRead('./hello2.txt', function (err, file) {
buffer = file;
});