测试learnyounode练习6时出现回调问题(使其模块化)

发布于 2025-01-14 02:10:50 字数 2236 浏览 3 评论 0原文

在测试 learnyounode(使其模块化)的练习 6 时,我的代码通过了所有测试,但只有一个测试并显示 TypeError:回调不是函数。

  ACTUAL                                 EXPECTED                
────────────────────────────────────────────────────────────────────────────────

   "CHANGELOG.md"                      ==    "CHANGELOG.md"                     
   "LICENCE.md"                        ==    "LICENCE.md"                       
   "README.md"                         ==    "README.md"                        

────────────────────────────────────────────────────────────────────────────────

 ✓ 

 Submission results match expected

Error
 ✓ 

 Additional module file exports a single function

 ✓ 

 Additional module file exports a function that takes 3 arguments

 ✓ 

 Additional module file handles errors properly

 ✓ 

 Additional module file handles callback argument

 ✓ 

 Additional module file returned two arguments on the callback function

 ✓ 

 Additional module file returned an Array as the second argument of the
 callback

data.dat
learnyounode.dat
w00t.dat
words.dat
 ✓ 

 Additional module file correctly handles '.'-prefixed extension

 ✗ 

 Your additional module file [mymodule.js] did not return an Array with the
 correct number of elements as the second argument of the callback

 # FAIL Your solution to MAKE IT MODULAR didn't pass. Try again!

─────────────────────────────────────────────────────────────────────────────

mymodule.js:

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

function extFunc(directory, ext, callbackFunc) {
    fs.readdir(directory, function(err, files) {
        if (err) {
            console.log("Error")
            return callbackFunc(err);
        }
        
        files.forEach(function(file) {
            if (path.extname(file) === ext) {
                console.log(file);
            };
        });
        callbackFunc(err, files);
    })
};

module.exports = extFunc;

make-it-modular.js

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

const directory = process.argv[2];
const ext = '.' + process.argv[3];

extFunc(directory, ext);

如果我能得到一些关于为什么 mymodule.js 中的回调函数抛出此错误并导致我最终无法通过 learnyounode 测试套件的反馈,我将不胜感激。

While testing exercise 6 of learnyounode (Make It Modular) my code is passing all of the tests but one and showing a TypeError: callback is not a function.

  ACTUAL                                 EXPECTED                
────────────────────────────────────────────────────────────────────────────────

   "CHANGELOG.md"                      ==    "CHANGELOG.md"                     
   "LICENCE.md"                        ==    "LICENCE.md"                       
   "README.md"                         ==    "README.md"                        

────────────────────────────────────────────────────────────────────────────────

 ✓ 

 Submission results match expected

Error
 ✓ 

 Additional module file exports a single function

 ✓ 

 Additional module file exports a function that takes 3 arguments

 ✓ 

 Additional module file handles errors properly

 ✓ 

 Additional module file handles callback argument

 ✓ 

 Additional module file returned two arguments on the callback function

 ✓ 

 Additional module file returned an Array as the second argument of the
 callback

data.dat
learnyounode.dat
w00t.dat
words.dat
 ✓ 

 Additional module file correctly handles '.'-prefixed extension

 ✗ 

 Your additional module file [mymodule.js] did not return an Array with the
 correct number of elements as the second argument of the callback

 # FAIL Your solution to MAKE IT MODULAR didn't pass. Try again!

─────────────────────────────────────────────────────────────────────────────

mymodule.js:

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

function extFunc(directory, ext, callbackFunc) {
    fs.readdir(directory, function(err, files) {
        if (err) {
            console.log("Error")
            return callbackFunc(err);
        }
        
        files.forEach(function(file) {
            if (path.extname(file) === ext) {
                console.log(file);
            };
        });
        callbackFunc(err, files);
    })
};

module.exports = extFunc;

make-it-modular.js

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

const directory = process.argv[2];
const ext = '.' + process.argv[3];

extFunc(directory, ext);

I'd be hugely grateful if I could get some feedback on why the callback function in mymodule.js is throwing this error and causing me to ultimately fail the learnyounode test suite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文