Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learnyounode #6 make it modular: correct results AND throwing error at the same time?

I'm completing the nodeschool.io learnyounode exercise #6, makeitmodular.

I'm getting the correct results, but there is still an error regarding a piece of code I'm not familiar with. Any help would be great.

Here are the results and error:

Your submission results compared to the expected:

                 ACTUAL                                 EXPECTED                
────────────────────────────────────────────────────────────────────────────────

   "CHANGELOG.md"                      ==    "CHANGELOG.md"                     
   "LICENCE.md"                        ==    "LICENCE.md"                       
   "README.md"                         ==    "README.md"                        
   ""                                  ==    ""                                 

────────────────────────────────────────────────────────────────────────────────

/usr/local/lib/node_modules/learnyounode/node_modules/workshopper-exercise/exercise.js:182
    processors[i].call(self, mode, function (err, pass) {
                 ^

TypeError: Cannot read property 'call' of undefined
    at next (/usr/local/lib/node_modules/learnyounode/node_modules/workshopper-exercise/exercise.js:182:18)
    at /usr/local/lib/node_modules/learnyounode/node_modules/workshopper-exercise/exercise.js:189:7
    at callback (/usr/local/lib/node_modules/learnyounode/exercises/make_it_modular/verify.js:26:15)
    at modFileError (/usr/local/lib/node_modules/learnyounode/exercises/make_it_modular/verify.js:31:5)
    at /usr/local/lib/node_modules/learnyounode/exercises/make_it_modular/verify.js:119:18
    at /Users/Olly/workspace/learnyounode/mymodule.js:13:13
    at Array.forEach (native)
    at /Users/Olly/workspace/learnyounode/mymodule.js:11:9
    at FSReqWrap.oncomplete (fs.js:82:15)

My makeitmodular.js file is:

var dir = process.argv[2];
var filter = process.argv[3];
var mymodule = require('./mymodule.js')


mymodule (dir,filter, function (err, data) {
    if (err) {
        console.log("There was an error")
    }
    else {
        console.log(data)
        }    

})

My module.js file is:

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

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

    fs.readdir(dir, function (err, list) {
        if (err) {
            return callback(err)
        }
        else {
            list.forEach( function(file) {
                if ( path.extname(file) === '.' + filter ) {
                    return callback(null, file)             
                }
            })
        }
    })




};
like image 663
leevigilstroy Avatar asked Feb 21 '16 21:02

leevigilstroy


1 Answers

I think the problem is that it expected that you call the callback function once with an array of the filtered list, and not every time in the forEach method.

----------Here's my solution in case you want to compare notes----------

My makeitmodular.js file is:

var path = require('path');
var mymodule = require('./mymodule');
var dir = process.argv[2];
var filterExtension = process.argv[3];

var callback = function (err, list) {
    if (err) throw err;
    list.forEach(function (file) {
        console.log(file);
    })
}

mymodule(dir, filterExtension, callback);

My module.js file is:

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

module.exports = function (directory, extension, callback) {
    fs.readdir(directory, function (err, list) {
        if (err) return callback(err);
        else {
            list = list.filter(function (file) {
                if(path.extname(file) === '.' + extension) return true;
            })
            return callback(null, list);
        }
    })
}
like image 107
Pokim Avatar answered Oct 21 '22 02:10

Pokim