开发者

Trying to walk a dropbox folder tree with node.js

I am trying to read dropbox metadata through their API, and write the url paths for ALL folders, subfolders and files into an array. Dropbox basically returns me a metadata response object showing all files and folders for a certain URL, and then I have to go into each folders again to do the same, until I have walked th开发者_开发知识库rough the whole tree.

Now the problem:

  • I have 'kind of' managed to walk the whole tree and do this, however due the way I did it I am unable to issue a call back(or event) when I have completed to walk through all possible urls.

  • Additionally I am calling a function from within itself. While this seems to work I do not know if it is a good or bad thing to do in Node.js. Any advice on this would also be appreciated, as I am fairly new to node.js.

My code:

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

Thanks!


OK, so I implemented a counter variable which increases every time the function is called, and decrements every time it has completed the loop. When the counter returns to zero an event gets dispatched. Not sure if it is a nice solution IMHO so if you anyone knows better please let me know. Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜