I am attempting to teach myself Node with the help of the popular LearnYouNode program. I am on exercise 6 titled 'Make it Modular'. Basically I have to create a Module, then require it in my main program.js and meet a few conditions.
One of these conditions says that in case of an error, I must invoke the callback function passing it only the error object and leaving the second parameter null.
Here is my module code so far:
var fs = require('fs'); // require file stream module
var pathMod = require('path'); // require path module
var i = 0; // local variable
var fileName = []; // local array variable
module.exports = function (path, ext, callback) {
return {
process: function () {
fs.readdir(path, callback);
},
success: function (ext, files) {
for(i; i < files.length; i++) {
var currentFile = files[i];
var currentFileExt = pathMod.extname(currentFile);
if(ext) {
if(ext === currentFileExt) {
fileName.push(currentFile);
}
} else {
fileName.push(currentFile);
}
}
return fileName;
}
};
};
And here is my main program code:
var prsFiles = require('./fpmod'); // require custom module
var path = process.argv[2]; // first parameter
var ext = '.' + process.argv[3]; // second parameter
var filtered; // local variable
// call back function
function callback(err, files) {
if(!err) {
// console.log('ext is: ' + ext)
filtered = mod.success(ext, files);
for (var i = 0; i < filtered.length; i++) {
console.log(filtered[i]);
};
} else {
console.log(err);
return;
}
}
var mod = prsFiles(path, ext, callback);
mod.process();
When I try to verify my code via LearnYouNode, my implementation fails with the message:
Your additional module file did not call the callback argument after an error from fs.readdir()
But I think my program does! What am I missing?
Thanks
The learnyounode tests expects your module to invoke the callback with either the array containing the list of files or an error that the readdir method might return. So, in place of 'return filename' the tests expect you to invoke the callback. Also, the tests seems to be very strict with the syntax because for me this code didn't work -->
if(err) {
callback(true)
} else {...}
But this worked -->
if(err) callback(err);
else {...}
With that being said here is the code that finally worked for me.
Module.js -->
module.exports = function(dir, ext, callback) {
var fs = require('fs');
var path = require('path');
fs.readdir(dir, function(err, data) {
if (err) callback(err);
else {
var files = [];
for (var i = 0; i < data.length; i++) {
if (path.extname(data[i]) == '.' + ext) {
files.push(data[i]);
};
};
callback(null,files);
}
})
}
And Modular.js -->
var module = require('./module');
module(process.argv[2], process.argv[3], function(err, data) {
if (err) {
console.log('Error!')
} else {
for (var i = 0; i < data.length; i++) {
console.log(data[i]);
};
}
})
Another thing that you might find helpful - https://github.com/frasertweedale/learnyounode-solutions
Here's the official solution in case you want to compare notes:
──────────────────────────────────────────────────────────────────────────────── solution.js:
var filterFn = require('./solution_filter.js')
var dir = process.argv[2]
var filterStr = process.argv[3]
filterFn(dir, filterStr, function (err, list) {
if (err)
return console.error('There was an error:', err)
list.forEach(function (file) {
console.log(file)
})
})
──────────────────────────────────────────────────────────────────────────────── solution_filter.js:
var fs = require('fs')
var path = require('path')
module.exports = function (dir, filterStr, callback) {
fs.readdir(dir, function (err, list) {
if (err)
return callback(err)
list = list.filter(function (file) {
return path.extname(file) === '.' + filterStr
})
callback(null, list)
})
}
────────────────────────────────────────────────────────────────────────────────