尝试使用 node.js 遍历 dropbox 文件夹树

发布于 2024-12-06 12:15:05 字数 1872 浏览 0 评论 0原文

我正在尝试通过他们的 API 读取 dropbox 元数据,并编写所有文件夹、子文件夹的 url 路径并将文件放入数组中。 Dropbox 基本上会返回一个元数据响应对象,显示某个 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!
};

谢谢!

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 through 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-12-13 12:15:05

好的,所以我实现了一个计数器变量,每次调用该函数时该变量都会增加,并且每次完成循环时都会递减。当计数器返回零时,将调度一个事件。
不确定这是否是一个很好的解决方案,恕我直言,所以如果您有人更了解,请告诉我。谢谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文