我正在完成nodeschool.io learnyounode练习#6,makeitmodular。
我得到了正确的结果,但是对于我不熟悉的一段代码仍然存在错误。任何帮助都会很棒。
以下是结果和错误:
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)
我的makeitmodular.js文件是:
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)
}
})
我的module.js文件是:
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)
}
})
}
})
};
答案 0 :(得分:13)
我认为问题在于它希望您使用已过滤列表的数组调用一次回调函数,而不是每次都在forEach方法中调用。
----------这是我的解决方案,以防你要比较笔记----------
我的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);
}
})
}
答案 1 :(得分:0)
我同意挑战的措辞可以更清楚。这是代码的另一个版本,以防其他人感兴趣:
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;
答案 2 :(得分:0)
我只用ES6编写了一个更清晰的答案,我同意其余的这个挑战有点不清楚。
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;
答案 3 :(得分:0)
这是我提供的代码,可为您提供确切的预期结果,并将匿名函数附加到导出对象:
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);
});
};
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);
答案 4 :(得分:0)
我想出了一个更简洁的解决方案:
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));
});