【发布时间】:2021-11-12 23:25:16
【问题描述】:
我的 NodeJS 脚本有问题。
基本上我想将每个文件路径添加到一个数组中,然后在 bash 控制台中显示它。
但是当我尝试时,它给了我undefined。
这是我的代码:
const { app, BrowserWindow } = require('electron');
const fs = require('fs');
const path = require('path');
function repList(){
var directoryPath = path.join('Q:/Programmes');
let forbiddenDir = [".VERSIONS", "INSTALL"];
fs.readdir(directoryPath, function (err, files) { //Scans the files in the directory
if (err) {
return console.log('Unable to scan directory: ' + err);
}
else{
files.forEach(function (file){ //Loops through each file
var name = directoryPath+"/"+file;
if(forbiddenDir.includes(file)){ //Don't accept the file if unvalid
console.log(`${file} is a forbidden name.`);
}
else{ //Filename is valid
fs.stat(name, (error, stats) => {
if (stats.isDirectory()) { //If directory...
tabRep.push(name); //... add the full filename path to the tabRep array
}
else if (error) {
console.error(error);
}
});
};
}); //End of loop
return tabRep; //<-- THIS RETURN DOESN'T WORK
}
});
}
app.whenReady().then(() => {
console.log(repList());
})
它给了我这个输出而不是 tabRep 的元素:
未定义
.VERSIONS 是一个禁止的名称。
INSTALL 是一个禁止名称。
在Programmes 文件夹内:
\程序
\.VERSIONS
\文件夹1
\文件1
\Folder2
\ 安装
\文件夹N
\文件N
如果有人能给我一些帮助,将不胜感激。
【问题讨论】: