2016-06-22 9 views
2

私は自分自身に入っているいくつかの非同期の難点を知っていますが、npmの様々なFTPパッケージのいずれかの.listメソッドからコールバック内に複数のファイルをダウンロードしようとしています。ノードFTP:ループ内の複数の非同期呼び出し

だから、このようなもの(注:ここでJSFTPは、私はその1と同じ問題を入手するには、ラッパーの周りnode-ftpです):

ftp = new JSFTP(conf.FTPConfig) 
    ftp.ls(conf.remoteFolder, (err, fileList) => { 
    if (err) return console.error(err) 

    for (let i=0; i<fileList.length; i++){ 
     file = fileList[i] 
     ftp.get(file.name, `./Downloaded/${file.name}`, err => { 
     if (err) return console.error(err) 
     console.log(`${file.name} copied.`) 
     }) 
    } 
    }) 

私はループを使用し、一つだけのファイルをダウンロードしていない場合、すべてうまく動作します。 しかし、ループして、私は次のエラーを取得し、私は(1つのまたは2つの空のファイルのプレースホルダーを除く)のいずれかのファイルを取得しない保つ:

(要するに):Error: 503 Bad sequence of commands、さらに以下:'Probably trying a PASV operation while one is in progress

ループ内のすべての私の.getコールを起動しているようです(明らかに、それらが完了するのを待つことなく、正常です)。しかし、このようにすべての.get呼び出しを実際に実行するのに失敗しますか? 非同期に並行して実行するだけではありませんか?つまり、表示されjsftpのソースを見た後

{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: 503 Bad sequence of commands. 
    at Ftp.parse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:257:11) 
    at Ftp.parseResponse (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:174:8) 
    at Stream.<anonymous> (C:\Git\SenecaFTP\FTPMonitor\node_modules\jsftp\lib\jsftp.js:146:24) 
    at emitOne (events.js:96:13) 
    at Stream.emit (events.js:188:7) 
    at ResponseParser.reemit (C:\Git\SenecaFTP\FTPMonitor\node_modules\duplexer\index.js:70:25) 
    at emitOne (events.js:96:13) 
    at ResponseParser.emit (events.js:188:7) 
    at readableAddChunk (_stream_readable.js:172:18) 
    at ResponseParser.Readable.push (_stream_readable.js:130:10) code: 503 } 
{ Error: connect ECONNREFUSED 192.168.100.161:61229 
    at Object.exports._errnoException (util.js:1007:11) 
    at exports._exceptionWithHostPort (util.js:1030:20) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14) 
    code: 'ECONNREFUSED', 
    errno: 'ECONNREFUSED', 
    syscall: 'connect', 
    address: '192.168.100.161', 
    port: 61229, 
    msg: 'Probably trying a PASV operation while one is in progress' } 

答えて

3

は、単一の接続を作成し、ひとつのソケットを使用しています:https://github.com/sergi/jsftp/blob/master/lib/jsftp.js#L120

これは、各コマンドの新しい接続を作成しないことを意味するが、A使用しています単一の接続を介してコマンドを送信します。基本的に回避するには、アップロードするファイルごとに新しいSFTPインスタンスを作成します。それはあるかもしれないと私は、このようなasyncようなフロー制御ライブラリのいくつかの並べ替えを使用して、または約束ベースのソリューションを利用することをお勧めしかし

ftp = new JSFTP(conf.FTPConfig) 
 
    ftp.ls(conf.remoteFolder, (err, fileList) => { 
 
    if (err) return console.error(err) 
 

 
    for (let i=0; i<fileList.length; i++){ 
 
     file = fileList[i] 
 
     new JSFTP(conf.FTPConfig).get(file.name, `./Downloaded/${file.name}`, err => { 
 
     if (err) return console.error(err) 
 
     console.log(`${file.name} copied.`) 
 
     }) 
 
    } 
 
    })

:基本的にはこれまでのコードを再加工

少しだけ管理しやすい。 Downloading multiple file from ftp site using node jsこれはたくさん助けいや

+0

、多くの感謝:

は、私が言及したasyncモジュールを使用して同様の溶液でSOにも同様の質問があります!私は上に述べた簡単なルートに行きました。私はおそらく、今後Asyncライブラリを試してみるべきです。しかし、今は、これは概念実証のために必要なものでした。再度、感謝します! – batjko

関連する問題