I'm calling a function scandir()
with a parameter to pass and a callback function. Once that functions complete I call that callback, however I get callback is not a function
. I don't understand where i'm going wrong, any help would be appreciated.
Script file:
scan_dir(working_dir, function(items){
console.log('done');
dir_items = items;
console.log('items', dir_items);
});
Functions file:
var scan_dir_count = 0;
function scan_dir(path, callback){
scan_dir_count++;
fs.readdir(path, function(err, files){
files.forEach(function(item, index){
//console.log(index, item);
if (fs.lstatSync(path + '/' + item).isDirectory()){
files.push(path + '/' + scan_dir(path + '/' + item));
}
});
scan_dir_count--;
if (scan_dir_count == 0){
console.log('end');
callback(files);
}
});
}
Thanks.
EDIT: Updated to 4castles response. No longer returns any errors, but the callback function doesn't seem to be firing...
var scan_dir_count = 0;
function scan_dir(path, callback){
scan_dir_count++;
fs.readdir(path, function(err, files){
files.forEach(function(item, index){
//console.log(index, item);
if (fs.lstatSync(path + '/' + item).isDirectory()){
scan_dir(path + '/' + item, function(data){ files.push(item + '/' + data); });
}
});
scan_dir_count--;
if (scan_dir_count == 0){
console.log('end');
console.log(files);
callback(files);
}
});
}
Are you sure that the function does not fire ? I gave the address of a sample folder and it seems to be working ok :
I got this as response :
end [ 'config.html', 'config.html~', 'config.js', 'config.js~' ] done items [ 'config.html', 'config.html~', 'config.js', 'config.js~' ]