[SOLVED] Problem with learnyounode exercise MAKE IT MODULAR

[SOLVED] Problem with learnyounode exercise MAKE IT MODULAR
0

#1

I cannot pass this excercise because it complaines about

  ✗  Your additional module file [readdir.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) 

When I manually try to execute the program to produce an error it reports it

[email protected] ~/devel/js/fcc/lynode % node program.js adkjvbas 
There was an error Error: ENOENT: no such file or directory, scandir 'adkjvbas'

Can someone help me to see what the problem is here?

//// file: program.js
const readdir = require("./readdir")
const args = [...process.argv].slice(2);

readdir(args[0], (err, data) => {
    if (err) {
        return console.log("There was an error " + err);
    }
    data.forEach((val) => console.log(val));
}, args[1]);

//// file: readdir.js
const fs = require("fs");
const path = require("path");

module.exports = (dirname, callback, ext) => {
    fs.readdir(dirname, (err, data) => {
        if (err) {
            return callback(err);
        }

        let filteredDir = data.filter((val) => {
            if (ext) {
                return path.extname(val) === "." + ext;
            } else {
                return true;
            }
        })
        callback(null, filteredDir);
    })
}

#2

I haven’t had my morning coffee yet, so I may be wrong…

I can see you calling a callback function, but you don’t actually define what that callback function should do anywhere.

Edit

I have sipped my coffee and think I may be barking up the wrong tree there…give it half a cup and I may have better insights…


#3

I solved it by trial and error.

Apparently the order of arguments matters. I wrote my function in a way that I can omit the filter extension (dirname, callback, ext). I changed it to (dirname, ext, callback) an then it worked.

I don’t understand it though.


#4

A post was split to a new topic: Learnyounode error