在 readdir 中使用 readdir 后如何向客户端发送数据
How can I send data to the client after use readdir inside a readdir
我需要读取一个名为 'curvas' 的文件夹。 'curvas' 中有很多文件夹,我正在使用 readdir 来获取它们。获取 'curvas' 内的文件夹后,我正在执行 foreach 并使用 readdir 列出文件('curvas' 内的文件夹内的文件)。
问题是我只能访问内部 readdir 中的变量(我用 console.log 测试过)。
问:如何访问这个变量并用 res 响应客户端。 ?
这是我的进展:
exports.getInserted = async function (req, res) {
var teste = await getFilesNames(); // HERE I NEED TO GET THIS FILES NAMES WITH FOLDERS
console.log(teste); // AND THEM PASSING TO THE CLIENT WITH RES.SEND()
};
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
var fileNames = [];
fs.readdir(chartFolder, (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
folders.forEach(folder => {
fs.readdir(chartFolder + folder, (err, files) => {
fileNames = files;
console.log(files) // ----- HERE I CAN SEE ALL FILES AND LIST WITH THEIR FOLDERS.
});
});
// I NEED USE resolve(fileNames) HERE, BUT IS EMPTY!!
});
});
};
谢谢!
[编辑]
解决方案: @omarwaleed 指导
exports.getInserted = async function (req, res) {
// Envia os arquivos das pastas de curvas para o cliente.
var data = await getFilesNames();
res.status(200).send(data);
};
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
// take care. I've turned it into an async function here
fs.readdir(chartFolder, async (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
let promiseArray = [];
folders.forEach(folder => {
promiseArray.push(
new Promise((resolve, reject) => fs.readdir(chartFolder + folder, (err, files) => {
if (err) {
reject(err);
}
resolve({
folder: folder,
files: files
});
}))
)
});
let results = await Promise.all(promiseArray);
resolve(results)
});
});
};
使用 resolve 代替注释 // I NEED USE resolve(fileNames) HERE, BUT IS EMPTY!!
将导致未定义的 resolve 语句,因为第二个 fs.readdir
将 运行 异步,这意味着目录将在您的代码之后读取解决。为了解决这个问题,我建议你用 promise 包装 fs.readdir ,你可以等待所有文件的读取并解决响应
您的函数如下所示
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
var fileNames = [];
// take care. I've turned it into an async function here
fs.readdir(chartFolder, async (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
let promiseArray = [];
folders.forEach( folder => {
promiseArray.push(
new Promise((resolve, reject)=>fs.readdir(chartFolder + folder, (err, files)=>{
if(err){
reject(err)
}
resolve(files)
}))
)
});
let results = await Promise.all(promiseArray);
resolve(results)
});
});
};
我需要读取一个名为 'curvas' 的文件夹。 'curvas' 中有很多文件夹,我正在使用 readdir 来获取它们。获取 'curvas' 内的文件夹后,我正在执行 foreach 并使用 readdir 列出文件('curvas' 内的文件夹内的文件)。
问题是我只能访问内部 readdir 中的变量(我用 console.log 测试过)。
问:如何访问这个变量并用 res 响应客户端。 ?
这是我的进展:
exports.getInserted = async function (req, res) {
var teste = await getFilesNames(); // HERE I NEED TO GET THIS FILES NAMES WITH FOLDERS
console.log(teste); // AND THEM PASSING TO THE CLIENT WITH RES.SEND()
};
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
var fileNames = [];
fs.readdir(chartFolder, (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
folders.forEach(folder => {
fs.readdir(chartFolder + folder, (err, files) => {
fileNames = files;
console.log(files) // ----- HERE I CAN SEE ALL FILES AND LIST WITH THEIR FOLDERS.
});
});
// I NEED USE resolve(fileNames) HERE, BUT IS EMPTY!!
});
});
};
谢谢!
[编辑]
解决方案: @omarwaleed 指导
exports.getInserted = async function (req, res) {
// Envia os arquivos das pastas de curvas para o cliente.
var data = await getFilesNames();
res.status(200).send(data);
};
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
// take care. I've turned it into an async function here
fs.readdir(chartFolder, async (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
let promiseArray = [];
folders.forEach(folder => {
promiseArray.push(
new Promise((resolve, reject) => fs.readdir(chartFolder + folder, (err, files) => {
if (err) {
reject(err);
}
resolve({
folder: folder,
files: files
});
}))
)
});
let results = await Promise.all(promiseArray);
resolve(results)
});
});
};
使用 resolve 代替注释 // I NEED USE resolve(fileNames) HERE, BUT IS EMPTY!!
将导致未定义的 resolve 语句,因为第二个 fs.readdir
将 运行 异步,这意味着目录将在您的代码之后读取解决。为了解决这个问题,我建议你用 promise 包装 fs.readdir ,你可以等待所有文件的读取并解决响应
您的函数如下所示
function getFilesNames() {
return new Promise(function (resolve, reject) {
const chartFolder = './curvas/';
var fileNames = [];
// take care. I've turned it into an async function here
fs.readdir(chartFolder, async (err, folders) => {
if (err) {
reject(new Error("Erro ao ler os arquivos."));
}
let promiseArray = [];
folders.forEach( folder => {
promiseArray.push(
new Promise((resolve, reject)=>fs.readdir(chartFolder + folder, (err, files)=>{
if(err){
reject(err)
}
resolve(files)
}))
)
});
let results = await Promise.all(promiseArray);
resolve(results)
});
});
};