3

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

ありがとう!

4

1 に答える 1

1

OK、関数が呼び出されるたびに増加し、ループが完了するたびに減少するカウンター変数を実装しました。カウンターがゼロに戻ると、イベントがディスパッチされます。それが良い解決策であるかどうかわからないので、誰かがもっとよく知っているなら、私に知らせてください。ありがとう。

于 2011-09-29T12:30:21.760 に答える