【问题标题】:UnhandledPromiseRejectionWarning when using a SoapClient使用 SoapClient 时出现 UnhandledPromiseRejectionWarning
【发布时间】:2019-08-17 19:40:24
【问题描述】:

我是 node.js 的新手,我最近正在试验一些东西。几天前,我尝试使用 easysoap-request 向 API 发送 XML 请求。它工作得很好,但我不得不为每个不同的查询创建一个 XML 文件,所以我尝试使用 easysoap。我发现自己很快就卡住了,但我设法在这个网站的帮助下解决了一些问题。现在我的程序给出了一个我难以理解的错误。这是我的代码:

const EasySoap = require('easysoap');
const request = (async () => {

    const params = {
        host    : 'https://someapi.com',
        path    : '/dir/soap',
        wsdl    : '/dir/wsdl',
        headers: [{
            'user-agent': 'Request-Promise',
            'Content-Type': 'text/xml',
        }]
    }

    var soapClient = EasySoap(params);

    soapClient.call({
        method    :'one_methode',
        attributes: {
            xmlns: 'https://someapi.com'
        },
        params: {
            'api' : {
                'authentication' : {
                    'login' : 'mylogin',
                    'password' : 'mypassword'
                },
                'params' : {
                    'another_params' : {
                        'name' : 'Brian',
                    }
                }
            }
        }
    }).then((callResponse) => {
        console.log(callResponse.data); // response data as json
        console.log(callResponse.body); // response body
        console.log(callResponse.header);  //response header
    }).catch((err) => { 
        throw new Error(err);
    });
});
request();

它给我的错误:

node:10264) UnhandledPromiseRejectionWarning: E​​rror: Error: no wsdl/xml response at soapClient.call.then.catch (C:\Users\user\Documents\src\script.js:40:15) at process._tickCallback (internal/process/next_tick.js:68:7) (node:10264) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝 id:1)(节点:10264)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

这是 .catch() 的问题吗?有人可以解释一下吗?谢谢

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    这是因为您在 catch 内抛出错误,它被包装到 Promise.reject 中,因为它被抛出在 async 函数内,而不是 catching 任何地方。

    ...
    }).catch((err) => { 
      throw new Error(err);
    });
    

    您可以像 console.error(err) 这样处理该块中的错误 或在您的 request() 函数调用中处理,如

    // request returns promise since you wrapped it in async function
    request()
      .catch(err => console.error(err))
    

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 2019-04-26
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多