I keep getting undefined
returned when I run this code:
var fs = require('fs'),
path = require('path'),
filepath = process.argv[2];
function listFilesByExtension(path_to_file, ext) {
fs.readdir(path_to_file, function (err, files) {
if (err) {
console.log(err);
} else {
console.log(files);
}
});
}
console.log(listFilesByExtension(filepath, 'txt'));
the console.log(files)
returns:
undefined
[ 'CHANGELOG.md',
'LICENCE.md',
'README.md',
'api.html',
'dat',
'data.dat',
'data.json',
'learnyounode.dat',
'learnyounode.sql',
'learnyounode.txt',
'md',
'w00t.dat',
'w00t.txt',
'words.dat',
'wrrrrongdat' ]
So the undefined
is not from there, console.log(err)
is also return null
.
Can anybody explain to me why this is happen? thank you