[英]Javascript function returning array empty
When I use the getAll() I am returned with an empty table.当我使用 getAll() 时,我返回一个空表。 I'm pretty sure its skipping everything inbetween the
let filesdir = []
and return filesdir
because after I push the files I have used console.log and it has logged the files, but I'm not sure why its completely skipping it我很确定它跳过了
let filesdir = []
和return filesdir
之间的所有内容,因为在我推送文件后,我使用了 console.log 并且它已经记录了文件,但我不确定它为什么完全跳过它
(Image of the directory, I havn't added https to it yet since I havn't bothered to look how to with cloudflare's ssl certifcate) (目录的图像,我还没有添加 https 到它,因为我还没有费心去看如何使用 cloudflare 的 ssl 证书)
getAll = async function (directory) {
let filesdir = [];
await fs.readdir(directory, async (err, files) => {
if (err) throw err;
for (i = 0; i < files.length; i++) {
var file = files[i];
//console.log(file)
var filePath = directory;
if (await fs.lstatSync(path.join(directory, file)).isFile()) {
filesdir.push('/' + file);
} else if (await fs.lstatSync(path.join(directory, file)).isFile()) {
var dd = getAll(path.join(filePath, file));
dd.then(function (ee) {
for (b = 0; b < ee.length; b++) {
filesdir.push('/' + ee[b]);
}
});
}
}
});
return filesdir;
};
getAll(pathDir).then(function (ee) {
console.log(ee);
for (i = 0; i < ee.length; i++) {
console.log(ee[i]);
}
});
var fs = require('fs');
var path = require('path');
getAll = async function (directory) {
let filesdir = [];
var files = fs.readdirSync(directory);
for (i = 0; i < files.length; i++) {
var file = files[i];
var filePath = directory;
if (await fs.lstatSync(path.join(directory, file)).isFile()) {
filesdir.push('/' + file);
} else if (await fs.lstatSync(path.join(directory, file)).isFile()) {
var dd = getAll(path.join(filePath, file));
dd.then(function (ee) {
for (b = 0; b < ee.length; b++) {
filesdir.push('/' + ee[b]);
}
});
}
}
return filesdir;
};
getAll('./').then(function (ee) {
console.log(ee);
for (i = 0; i < ee.length; i++) {
console.log(ee[i]);
}
});
// Or
var ee = await getAll('./');
for (i = 0; i < ee.length; i++) {
console.log(ee[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.