4

I am using node -v v14.17.0 and "ssh2-sftp-client": "^7.0.0" and method fastPut https://github.com/theophilusx/ssh2-sftp-client#sec-5-2-9

Checking the remote files is okay, so connection works.

My environment is wsl2 Ubuntu-20.04

Problem I face is error

RuntimeError: abort(Error: fastPut: No response from server Local: /home/draganddrop/testi.txt Remote: Downloads/testi.txt). Build with -s ASSERTIONS=1 for more info.
    at process.J (/home/draganddrop/node_modules/ssh2/lib/protocol/crypto/poly1305.js:20:53)
    at process.emit (events.js:376:20)
    at processPromiseRejections (internal/process/promises.js:245:33)
    at processTicksAndRejections (internal/process/task_queues.js:96:32)

I have tried also with sftp> put /home/draganddrop/testi.txt Downloads/testi.txt from console, which works.

Code I am using:

        let Client = require('ssh2-sftp-client');
        let sftp = new Client();
    
        let remotePath = 'Downloads/testi.txt';
        let localPath = '/home/draganddrop/testi.txt'

        const config = {
          host: 'XX.XX.XXX.XXX',
          port: '22',
          username: 'XXXXX',
          password: 'XXXXXX'
        };

        sftp.connect(config)
        .then(() => {
          sftp.fastPut(localPath, remotePath);
          //return sftp.exists(remotePath);
        })
        //.then(data => {
        //  console.log(data);          // will be false or d, -, l (dir, file or link)
        //})
        .then(() => {
          sftp.end();
        })
        .catch(err => {
          console.error(err.message);
        });

I have no idea what causes this error, I've tried with different paths and get either bad path error or this. What could be the cause?

eemilk
  • 1,375
  • 13
  • 17
  • 2
    Hi @eemilk, looks like the problem in asynchronous function fastPut and second `then`, it close connecting before put the file, try to return promise in first then `return sftp.fastPut(localPath, remotePath);` – Pavlo Naumenko Jul 18 '21 at 09:19
  • @PavloNaumenko This seem to have done the trick. – eemilk Jul 18 '21 at 13:03
  • I wrote the same in the answer – Pavlo Naumenko Jul 18 '21 at 13:42
  • Please help on this https://stackoverflow.com/questions/72277310/node-js-ssh2-sftp-client-error-fastput-unhandledpromiserejectionwarning-error – its me May 17 '22 at 16:04

1 Answers1

5

The reason for the problem is that the connection is closing before the finish of executing fastPut.
You are running connect, after that in the first .then, the method fastPut runs asynchronously, it doesn't wait for the finish execution and return undefined to the next method .then of the chain.
To solve the problem you just need to return a promise received from fastPut

sftp.connect(config)
        .then(() => sftp.fastPut(localPath, remotePath))
        .then((data) => {/* do something*/}
        .finally(() => sftp.end())
Pavlo Naumenko
  • 561
  • 5
  • 15
  • First 'then' should return? return sftp.fastPut.... – BertC Oct 18 '21 at 09:00
  • Sorry @BertC, I didn't get the question. Callback in first `then` is arrow function expression, so it returns result of `sftp.fastPut` – Pavlo Naumenko Oct 18 '21 at 10:07
  • 1
    Yes @Pavlo, you are absolutely right. I missed the missing curly braces. Sorry for that. – BertC Oct 18 '21 at 10:19
  • please help on this https://stackoverflow.com/questions/72277310/node-js-ssh2-sftp-client-error-fastput-unhandledpromiserejectionwarning-error – its me May 17 '22 at 16:05