Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAKE IT MODULAR fails due to "wrong" order of function parameters #465

Open
pnahratow opened this issue Aug 30, 2016 · 0 comments
Open

MAKE IT MODULAR fails due to "wrong" order of function parameters #465

pnahratow opened this issue Aug 30, 2016 · 0 comments

Comments

@pnahratow
Copy link

  • node version: v6.5.0
  • npm version: 3.10.7
  • learnyounode version: 3.5.5

Error output

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

My Code

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

 readdir(args[0], (err, data) => {
     if (err) {
         return console.log(err);
     }
     data.forEach((val) => console.log(val));
 }, args[1]);

 //// file: readdir.js
 module.exports = (dirname, callback, ext) => {
    const fs = require("fs");
    const path = require("path");

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

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

        callback(null, filteredDir);
    })
}

Notes

Producing an error manually works as intended

philipp@debian-acer5750g ~/devel/js/fcc/lynode % node program.js madeupdirectory
{ Error: ENOENT: no such file or directory, scandir 'madeupdirectory'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'scandir',
  path: 'madeupdirectory' }

Problem

The check seems to fail due to the unexpected order of function parameters (dirname, callback, ext).
I wrote it this way because I thought it might be useful if I can just omit the ext parameter.
When changing the order of parameters to (dirname, ext, callback) the test passes.
I think the code I provided should pass too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant