gpt4 book ai didi

javascript - Learnyounode #6 使其模块化 : correct results AND throwing error at the same time?

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:25 24 4
gpt4 key购买 nike

我正在完成 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)
}
})
}
})




};

最佳答案

我认为问题在于它希望您使用过滤列表的数组调用一次回调函数,而不是每次都在 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);
}
})
}

关于javascript - Learnyounode #6 使其模块化 : correct results AND throwing error at the same time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com