0

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?

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
  • Wearing my guessing hat, but would a semi colon at the end of the return statement help in this case? – Gruff Bunny Nov 27 '13 at 20:50
  • It's usual, if you're not using curly braces to structure your code, to have the `return` on the same line as the `if`. Perhaps it's that. – Andy Nov 27 '13 at 20:52
  • Thanks for the answers but neither of those changed the result. I will update my question with new code. – Fedor Hajdu Nov 27 '13 at 20:55
  • @GruffBunny seemingly little known fact, Javascript does not require the `;` to terminate a line. http://stackoverflow.com/questions/4002234/do-we-need-semicolon-at-the-end – Justin Wood Nov 27 '13 at 21:06
  • @JustinWood The suggestion was more targeted towards whatever was parsing the js to give the error message. Nevertheless, it's good practice to add the semi colon otherwise semi colon insertion could cause unexpected behaviour. – Gruff Bunny Nov 27 '13 at 22:25

1 Answers1

1

callback is undefined, so you can't call it like a function. I'm not sure what the correct code is, by I suspect that changing your first line to:

module.exports = function (dir, filter, callback){

will fix your problem.

Paul
  • 139,544
  • 27
  • 275
  • 264