Make It Modular Verification Issue
ScidaJ opened this issue · 4 comments
Hi, I was using this library to brush up on my Node knowledge and I ran into an issue with the error handling verification on the Make It Modular activity.
This is my main file:
'use strict'
const mymodule = require('./MyModule.js');
function printResults(err, results){
if(err) return console.error(err);
results.forEach((item, i) => {
console.log(item);
});
}
mymodule(printResults, process.argv[3], process.argv[2]);
And this is my module file.
'use strict'
const fs = require('fs');
const path = require('path');
let arr = [];
module.exports = function (callback, ext, dir){
ext = '.' + ext;
fs.readdir(dir, 'utf8', function (err, data){
if(err){
return callback(err);
}
data.forEach(function(file){
if(path.extname(file) === ext){
arr.push(file);
}
})
return callback(null, arr);
});
}
I don't understand why it's not passing the error handling tests, as when I pass in args to trigger errors(i.e. /foo/bar as the path and wheee as the extension) they are all handled and returned back to my main file. Any help would be appreciated.
Could you please post the exact error message/test you’re failing on? That might help narrow it down 🙂
This is the test that I'm failing on.
Your additional module file [MyModule.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)
It might be due to the order of your parameters in your exported function? I think the problem specifies that the function should conform to the contract of
dir
, ext
, and callback
.
That was it. That is incredibly annoying as I thought I was doing something extremely wrong and even had a coworker more experienced in Node take a look at it and he didn't see anything wrong. Appreciate your help.
I'm going to post the code that works below.
MakeItModular.js
'use strict'
const mymodule = require('./MyModule.js');
function printResults(err, results){
if(err) return console.error(err);
results.forEach((item, i) => {
console.log(item);
});
}
mymodule(process.argv[2], process.argv[3], printResults);
MyModule.js
'use strict'
const fs = require('fs');
const path = require('path');
let arr = [];
module.exports = function (dir, ext, callback){
ext = '.' + ext;
fs.readdir(dir, 'utf8', function (err, data){
if(err){
return callback(err);
}
data.forEach(function(file){
if(path.extname(file) === ext){
arr.push(file);
}
})
return callback(null, arr);
});
}