最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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




};

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




};
Share Improve this question asked Feb 21, 2016 at 21:18 leevigilstroyleevigilstroy 6046 silver badges11 bronze badges 6
  • I had same problem, I think it's workshop's bug, so you should create issue on Github – Sergei Stralenia Commented Feb 21, 2016 at 21:20
  • Are you sure you want args 2 & 3 and not 1 & 2 ? – 8eecf0d2 Commented Feb 21, 2016 at 21:22
  • @brod, I was informed arg 1 returns username, and arg 2 returns path of the learnyounode folder. – leevigilstroy Commented Feb 21, 2016 at 21:28
  • hmm - so this is what you're expecting: $ command with args argv[0] == 'command' argv[1] == 'with' argv[2] == 'args' – 8eecf0d2 Commented Feb 21, 2016 at 22:08
  • This looks like a bug in the learnyounode module. It might be related to the fact that you're calling callback multiple times, but it still should not happen and throw a more descriptive error message instead. Report it to the learnyounode devs. – Bergi Commented Feb 21, 2016 at 22:56
 |  Show 1 more comment

5 Answers 5

Reset to default 17

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

I coded a clearer answer with only ES6, I agree with the rest this challenge is a little 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;

I agree that the wording of the challenge could be more clear. Here's another version of the code in case anyone else is interested:

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;

Here is the code that I have come up with that gives you the exact expected results and attaches an anonymous function to the exports object:

filtered.js (module)

module.exports = function (dir, ext, callback) {
  var fs = require('fs');
  var path = require('path');
  var filteredList = [];
  var currentFile;
  var currentExt;
  var x;

  fs.readdir(dir, function (err, list) {
    if (err) {
      return callback(err);
    }

    for (x in list) {
      currentFile = list[x];
      currentExt = path.extname(currentFile);

      if (currentExt === '.' + ext) {
        filteredList.push(currentFile);
      }
    }
    return callback(null, filteredList);
  });
};

make-it-modular.js (program)

var filter = require('./filtered');
var file = process.argv[2];
var ext = process.argv[3];

var callback = function (err, list) {  
  for (x in list) {
    console.log(list[x]);
  }
}

filter(file, ext, callback);

I was able to come up with a bit more condensed solution:

mymodule.js

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

module.exports = function (dir, ext, cb) {
    fs.readdir(dir, (err, data) => {
        if(err) return cb(err);
        
        cb(
            null,
            data.filter(file => path.extname(file) === `.${ext}`)
        );
    });
} 

make-it-modular.js

const mod = require('./mymodule');

const dir = process.argv[2];
const ext = process.argv[3];

mod(dir, ext, (err, data) => {
    if(err) console.log(err);
    data.forEach(element => console.log(element));
});
发布评论

评论列表(0)

  1. 暂无评论