JSON数据返回{},没有任何数据

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和该艺术品文件夹内包含文件artdetails.txt,artinfo.txt创build文件夹包含文件creationdetails.txt,create.txt等。

文件夹和文件取得成功,但我想要生成一个JSON格式,如下所示:

 {`art':['artdetails',artinfo],'creation':['creationdetails','create']} format 

怎么可能?

我用resultingJson[files[j]] = fi; 但它返回{}.

我的代码有什么问题?

这里有几个问题。 首先,Felix Kling对readdir做了asynchronous的更正确的观察,更具体地说,是指for循环内的readdir 。 你看到的一部分是你的console.log和JSON响应在目录读完之前就已经发生了。 另外可能发生的是j的背景正在丢失,可能会结束最后的价值。

像async这样的控制stream库可能会有所帮助,比如每种方法。

 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); }); }); }; 

我已经通过嘲笑你的文件夹/文件结构来testing它,它似乎正常工作。 它生成了以下JSONstring:

 {"art":["artdetails.txt","artinfo.txt"],"creation":["create.txt","creationdetails.txt"]} 

迭代器被并行调用,所以顺序可能会切换,但不应该有所作为。

您关心的是将recursion函数中的resultsJson的值重置为{}。

试试这个代码

 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); }); } 

请尝试另一种方式:

 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); });