var getfiles = function (context) {
var scriptPath = '/var/names/myfolder';
fs.readdir(scriptPath, function (err, files) {
if (err) return context.sendJson(err, 404);
var resultingJson = {};
for (var j = 0; j < files.length; j++) {
subfolder = scriptPath + files[j];
console.log(files[j]);// prints art,creation
fs.readdir(subfolder, function (err, fi) {
//fi prints [artdetails.txt,artinfo.txt]
// [creationdetails.txt,create.txt]
// console.log(files[j]);// prints undefined here
resultingJson[files[j]] = fi;
});
}
console.log(resultingJson); // returing {}
context.sendJson(resultingJson, 200);
});
}
上面的代码用于获取子文件夹myfolder中的文件,它包含art,creation
,在此art文件夹中包含文件artdetails.txt,artinfo.txt
创建文件夹包含文件creationdetails.txt,create.txt
等。
文件夹和文件已成功获取,但我想生成JSON
格式,如下所示:
{`art':['artdetails',artinfo],'creation':['creationdetails','create']} format
怎么可能?
我使用resultingJson[files[j]] = fi;
但它返回{}.
我的代码出了什么问题?
答案 0 :(得分:2)
您需要在递归函数中将resulJson的值重置为{}。
试试这段代码
var getfiles = function (context) {
var scriptPath = '/var/names/myfolder';
var resultingJson = {};
fs.readdir(scriptPath, function (err, files) {
if (err) return context.sendJson(err, 404);
for (var j = 0; j < files.length; j++) {
subfolder = scriptPath + files[j];
console.log(files[j]);// prints art,creation
fs.readdir(subfolder, function (err, fi) {
//fi prints [artdetails.txt,artinfo.txt]
// [creationdetails.txt,create.txt]
// console.log(files[j]);// prints undefined here
resultingJson[files[j]] = fi;
});
}
console.log(resultingJson); // returing {}
context.sendJson(resultingJson, 200);
});
}
答案 1 :(得分:1)
这里有一些问题。首先,Felix Kling正确地观察了readdir
是异步的,更具体地说是指for循环中的内部readdir
。您所看到的部分原因是您的console.log和JSON响应在目录读完之前发生。同样可能发生的是j
的上下文正在丢失,可能会结束最后一个值。
var fs = require('fs'),
async = require('async');
var scriptPath = '/var/names/myfolder';
var getfiles = function(context) {
// Read contents of the parent directory
fs.readdir(scriptPath, function(err, files) {
if (err) return context.sendJson(err, 404);
var result = {};
// Asynchronously iterate over the directories
async.each(files, function iterator(directory, next){
var subfolder = scriptPath + directory;
// Read contents of the child directory
fs.readdir(subfolder, function(err, file){
if (err) return next(err);
// Set the property
result[directory] = file;
// Now that we've finished reading these contents,
// lets read the contents of the next child folder
next();
// When there are none left, the `complete` callback will
// be reached and then it is safe to return a JSON string
});
}, function complete(err){
// All children directories have been read
// and the `result` object should be what we expect
if (err) return context.sendJson(err, 404);
context.sendJson(result, 200);
});
});
};
我已经通过模拟您的文件夹/文件结构来测试它,它似乎正常工作。它生成了以下JSON字符串:
{"art":["artdetails.txt","artinfo.txt"],"creation":["create.txt","creationdetails.txt"]}
迭代器是并行调用的,所以顺序可以切换,但它不应该有所作为。
答案 2 :(得分:0)
请尝试另一种方式:
fs.readdir(scriptPath, function (err, files) {
if (err) return context.sendJson(err, 404);
files.forEach(function(file) {
subfolder = scriptPath + file;
console.log(file);
fs.readdir(subfolder, function (err, fi) {
resultingJson[file] = fi;
});
}
console.log(resultingJson);
context.sendJson(resultingJson, 200);
});