我正在尝试通过他们的API读取保管箱元数据,并将所有文件夹、子文件夹和文件的 url 路径写入数组。Dropbox 基本上会返回一个元数据响应对象,显示某个 URL 的所有文件和文件夹,然后我必须再次进入每个文件夹以执行相同操作,直到我遍历整个树。
现在的问题:
我已经“有点”设法走过整棵树并这样做了,但是由于我这样做的方式,当我完成遍历所有可能的 URL 时,我无法发出回调(或事件)。
此外,我正在从自身内部调用一个函数。虽然这似乎可行,但我不知道在 Node.js 中这样做是好事还是坏事。对此的任何建议也将不胜感激,因为我对 node.js 相当陌生。
我的代码:
function pathsToArray(metadataarr,callback){ //Call this function and pass the Dropbox metadata array to it, along with a callback function
for (aItem in metadataarray ){ //For every folder or file in the metadata(which represents a specific URL)
if (metadataarr[aItem].is_dir){ //It is a folder
dropbox.paths.push(metadataarr[aItem].path+"/"); //Write the path of the folder to my array called 'dropbox.paths'
dropbox.getMetadata(metadataarr[aItem].path.slice(1),function(err, data){ //We go into the folder-->Call the dropbox API to get metadata for the path of the folder.
if (err){
}
else {
pathsToArray(data.contents,function(err){ //Call the function from within itself for the url of the folder. 'data.contents' is where the metadata returned by Dropbox lists files/folders
});
}
});
}
else { //It is a file
dropbox.paths.push(metadataarr[aItem].path); //Write the path of the file to my array called 'dropbox.paths'
}
}
return callback(); //This returns multiple times, instead of only once when everything is ready, and that is the problem!
};
谢谢!