Search code examples
javascriptnode.jssshsftpssh2-sftp-client

Node.js - ssh2-sftp-client getting multiple files error


I use ssh2-sftp-client to get some files. If I use sftp.end(); at the end I get this error in the console:

{ Error: fastGet->exists->exists: No SFTP connection available
    at Object.formatError (/home/project/node_modules/ssh2-sftp-client/src/utils.js:62:18)
    at SftpClient.fastGet (/home/project/node_modules/ssh2-sftp-client/src/index.js:590:19)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: 'ERR_NOT_CONNECTED', custom: true } 'Error'

...

What am I missing?


Solution

  • What I believe is currently happening is that since you're not returning a Promise from your second then(), the third then() gets resolved immediately (with a value of undefined), which causes your connection to get terminated immediately (before your fastGet()s have time to finish).

    To fix this, you would need to explicitly return a Promise from your second then() that only gets resolved once all of the files have been transferred. You may also want to consider rejecting that promise if at least one of the transfers fails.