以下是我的代码片段。我正在尝试创建一个名为photoarray的数组,该数组将为每个包含每个目录的所有文件的目录保存数组。但是,当我运行它时,我收到一个错误。当我在没有for循环的情况下运行它并且硬编码目录被分析为directoriesparsed [0]时,它正常工作。请帮助,谢谢。
var photoarray = [];
for(z=0; z<directoriesparsed.length; z++){
var Name = [];
var d = "../public/images/Accepted/" + directoriesparsed[z];
console.log(d);
fs.readdir(d, function (err, files) {
if (err) {
throw err;
}
files.map(function (file) {
return path.join(d, file);
}).filter(function (file) {
return fs.statSync(file).isFile();
}).forEach(function (file) {
console.log(file);
Name.push(file);
console.log(Name);
});
photoarray.push(Name);
console.log(photoarray);
});
}
fs.js:981 return binding.stat(pathModule._makeLong(path)); ^ Error: ENOENT: no such file or directory, stat '../public/images/Accepted/Scott/duncan1.jpg' at Error (native) at Object.fs.statSync (fs.js:981:18) at /home/pi/Node/webserverproject/myapp/backendfiles/testingjson2.js:56:25 at Array.filter (native) at /home/pi/Node/webserverproject/myapp/backendfiles/testingjson2.js:55:13 at FSReqWrap.oncomplete (fs.js:117:15)