Learnyounode # 6 makes it modular: correct results And throwing error at the same time?

I am completing exercise 6 for students at school.io learnyounode, makeitmodular.

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

Below are the results and errors:

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:

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:

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)             
                }
            })
        }
    })




};
+4
source share
3 answers

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

---------- Here is my solution in case you want to compare notes ----------

makeitmodular.js:

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);

module.js:

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);
        }
    })
}
+13

, . , - :

filter.js:

const path = process.argv[2]
const fileType = process.argv[3]
const readNewlines = require('./readNewlines')

readNewlines(path, fileType, function(err, result) {
    if(err) return err;
    console.log(result.join('\n'));
})

readNewlines.js:

const fs = require('fs')

readNewlines = function(path, fileType, callback) {
    fs.readdir(path, function(err, result) {
        if(err) return callback(err);

        const res = result.filter(function(fileName) {
            return (fileName.indexOf('.'+fileType) !== -1);
        }).map(fileName => {
            return fileName
        })

        return callback(null, res);
    })
}

module.exports = readNewlines;
0

I encoded a clearer answer only with ES6, I agree with the rest, this problem is a bit unclear.

program.js

const myModule = require('./myModule.js');
const dir = process.argv[2];
const ext = process.argv[3];

myModule(dir, ext, (err, list) => {
    return err ? console.error('There is an error:', err) : 
    console.log(list.join('\n'));
});

myModule.js

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

const myModule = (dir, ext, cb) => {
    fs.readdir(dir, (err, list) => {
        return err ? cb(err) : cb(null, list.filter(file => 
        path.extname(file) === `.${ext}`));
    });
};

module.exports = myModule;
0
source

Source: https://habr.com/ru/post/1629811/


All Articles