Я пытаюсь прочитать метаданные dropbox через их API и записать URL-пути для ВСЕХ папок, подпапок и файлов в массив. Dropbox в основном возвращает мне объект ответа метаданных, показывающий все файлы и папки для определенного URL-адреса, и затем мне приходится снова заходить в каждую папку, чтобы сделать то же самое, пока я не пройдусь по всему дереву.
Теперь проблема:
Мне «удалось» пройтись по всему дереву и сделать это, однако из-за того, как я это сделал, я не могу выполнить обратный вызов (или событие), когда я закончил, чтобы пройти все возможные URLs.
Кроме того, я вызываю функцию изнутри себя. Хотя это, кажется, работает, я не знаю, хорошо это или плохо в 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!
};
Спасибо!