0
votes

Trying to run a sftp client using nodejs but getting a wired error. The error is

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

let Client = require('ssh2-sftp-client');
let sftp = new Client();
const process = require('process');

const config = {
    host: 'localhost',
    port: '1026',
    username: 'Nav****',
    password: '*******'
}



sftp.connect(config)


const list = ()=>{
    sftp.connect(config).then(() => {
           return sftp.list('../send_backend');
    }).then((data) => {
            console.log(data, 'the data info');
    }).catch((err) => {
            console.log(err, 'catch error');
    })
}
console.log("=======================================");

console.log("received data =>" + list());
1

1 Answers

0
votes

What is "sftp.connect(config)" doing outside the list. As I can see you are neither using resolve nor the rejected case. That is causing the issue. So you should either handle the exception or remove that code!

In case you want to handle it

sftp.connect(config)
  .then(()=>{//do something here})
  .catch((exception)=>{ console.log(exception) // do something else })

As discussed in comments, You may use this:

let Client = require('ssh2-sftp-client');
let sftp = new Client();
const process = require('process');
const config = {
    host: 'localhost',
    port: '1026',
    username: 'Nav****',
    password: '*******'
}
sftp.connect(config)
  .then(()=>{
    // what you want to do after you make connection goes here
  })
  .catch((exception)=>{ 
       console.log(exception) // do something else
   })