使用SoapClient时的UnhandledPromiseRejectionWarning

时间:2019-03-27 09:30:57

标签: javascript node.js

我是node.js的新手,最近正在尝试一些东西。几天前,我尝试使用easysoap-request将XML请求发送到API。它工作得很好,但是我不得不为每个不同的查询创建一个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:错误:错误:在process._tickCallback处soapClient.call.then.catch(C:\ Users \ user \ Documents \ src \ script.js:40:15)处无wsdl / xml响应(内部/进程/next_tick.js:68:7)(节点:10264)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)(节点:10264)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

.catch()是否有问题?有人可以向我解释吗?谢谢

1 个答案:

答案 0 :(得分:0)

这是因为您在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))