Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我在Mocha框架中调用helper函数时,它在test.js中调用时返回Undefined_Javascript_Node.js_Mocha.js - Fatal编程技术网

Javascript 当我在Mocha框架中调用helper函数时,它在test.js中调用时返回Undefined

Javascript 当我在Mocha框架中调用helper函数时,它在test.js中调用时返回Undefined,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我有我的helper.js,我在其中定义了读取目录中文件名的函数: module.exports.getfilenames= function(dirPath) { console.log(dirPath); let files= fs.readdir(dirPath, function (err, files) { if (err) console.log(err); else { console.log("\nCu

我有我的helper.js,我在其中定义了读取目录中文件名的函数:

module.exports.getfilenames= function(dirPath)
{
    console.log(dirPath);
    let files= fs.readdir(dirPath, function (err, files) {
    if (err)
        console.log(err);
      else {
        console.log("\nCurrent directory filenames:");
        files.forEach(file => {
          console.log(file); 
        })
        return files;
      }
    }) 
};

In test.js
I am calling helper function as:
describe('FILES', function()
{
     files=helper.getfilenames(dirPath);//dirPath is a value of path of directory
     it('GET FILES', function(done) {        
     console.log("reading files:"+ files);
     done();
  })
})
输出:

reading files: Undefined

请建议如何解析test.js中的文件对象?

fs.readdir
是一个异步函数,您需要使用同步版本:


否则,您可以使用异步版本,但需要进行一些代码更改。

对readdir方法的同步调用解决了此问题。谢谢
let files = fs.readdirSync(dirPath);
files.forEach(file => {
  console.log(file); 
})
return files;