I'm working on a learnunode tutorial, and got stuck at a question so I'd like your help, since the software doesn't provide detailed enough answers.
I'm on a step "make it modular" that asks to separate code into modules. My module is:
module.exports = function (dir, filter, err){
var fs = require("fs");
var re = new RegExp('\\.' + filter + '$');
fs.readdir(dir, function(err, list) {
if (err) return callback(err);
for (var i=0; i < list.length; i++) {
var name = list[i];
if(name.match(re))
console.log(list[i]);
};
});
}
but the tutorial says it's not correct with message:
Your additional module file: /home/user/learnunode/submodule.js does not appear to pass back an error received from
fs.readdir()
Use the following idiomatic Node.js pattern inside your callback to
fs.readdir()
: if (err) return callback(err)FAIL
But I am using those lines. What am I doing wrong?
EDIT: The calling module is this: var mod = require('./submodule');
var mod = require('./submodule');
var folder = process.argv[2];
var extension = process.argv[3];
mod(folder, extension);
Maybe I should've caught the error or declare it and pass to the mod?