【问题标题】:Getting error UnhandledPromiseRejectionWarning收到错误 UnhandledPromiseRejectionWarning
【发布时间】:2019-11-02 21:09:00
【问题描述】:

尝试使用nodejs 运行sftp 客户端,但出现有线错误。错误是

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号: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());

【问题讨论】:

    标签: node.js sftp


    【解决方案1】:

    “sftp.connect(config)”在列表之外做什么。正如我所看到的,您既没有使用解决方案,也没有使用被拒绝的案例。这就是问题所在。因此,您应该处理异常或删除该代码!

    如果你想处理它

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

    正如在 cmets 中所讨论的,你可以使用这个:

    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
       })
    

    【讨论】:

    • 那么我该如何处理这里的异常呢?
    • 第一件事为什么你还需要那个位!
    • 问题是我想要一个 sftp 客户端,我在某处看到了这个 sn-p 代码,所以我只是复制并粘贴......我不知道这里到底发生了什么。
    • 整个部分已被复制。
    • 在哪里可以找到?
    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 2018-02-23
    • 2021-11-17
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2021-12-10
    • 2019-06-09
    相关资源
    最近更新 更多