shintaroid shintaroid - 1 year ago 125
Node.js Question

cannot pass learnyounode: is the callback function used uncorrectly?

I am trying to solve the sixth problem of learnyounode which needs a module file to print file list. Here are my two files:

The main file

program.js
:

var mymodule = require('./module.js');

mymodule(process.argv[2], process.argv[3], function(err, file){
if(err){
console.log(err);
return;
}
console.log(file);
});


The module file
module.js
:

var fs = require('fs');
var path = require('path');

var fileExt;

module.exports = function(dir, ext, callback) {
fs.readdir(dir, function(err, files){
if(err){
callback(err);
return;
}
files.forEach(function(file){
fileExt = path.extname(file).substring(1);
if(fileExt === ext){
callback(null, file);
}
});
});
}


But it throws an error:

processors[i].call(self, mode, function (err, pass) {
TypeError: Cannot read property 'call' of undefined


What am I doing wrong?

Answer Source

The instructions state that you need to call callback only once, with an array containing of all the matching files. In your case, you are calling callback once for every matching file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download