I'm new to NodeJS and I#m trying to read a directory recursively this is my code
var fs = require('fs');
var readDir = function (dir, calback) {
fs.readdir(dir, function (err, files) {
if (err)
console.log(err);
for (var file in files) {
fs.stat(dir + "/" + file, function (err, stats) {
if (err)
console.log(err);
if (stats.isFile()) {
calback(dir + "/" + file);
}
if (stats.isDirectory()) {
walk(file, calback);
}
});
}
});
};
This is my ErrorMessage
C:\Users\Lukas\Desktop\Enide-Studio-05-kepler-win32\ws\PlayerTest\hello-world-server.js:24
if (fs.stats.isFile()) {
^
TypeError: Cannot call method 'isFile' of undefined
at C:\Users\Lukas\Desktop\Enide-Studio-05-kepler-win32\ws\PlayerTest\hello-world-server.js:24:30
at Object.oncomplete (fs.js:107:15)
What is my mistake??
You are not exiting your function after encountering an error.
fs.stat(dir + "/" + file, function (err, stats) {
if (err) {
console.log(err);
return; // exit here since stats will be undefined
}
if (stats.isFile()) {
calback(dir + "/" + file);
}
if (stats.isDirectory()) {
walk(file, calback);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With