我很难理解如何在JS中操作堆栈 希望这项练习对我有帮助。
因此,我试图编写一个函数来进行soap xml调用并解析数据。然后在调用时返回解析的数据。
当我调用函数时,我可以将其保存到console.log结果中,但是我无法获取它以返回结果,因此可以将其存储在变量ect中。它返回未定义的结果。
下面的代码:
// find all O's connected to left and right borders, and change them to E's
答案 0 :(得分:1)
您应该使用https://codepen.io/baadaa/pen/WNbmoNK对将来的事件做出反应并处理JavaScript中异步代码的执行:
var request = require('request');
var DOMParser = require('xmldom').DOMParser;
function query(entity, field, op, expression, elementToParse) {
xml =
`<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_6/">
<IntegrationCode>H2FATPVKPDN25IXIBSMN5K66XAA</IntegrationCode>
</AutotaskIntegrations>
</soap:Header>
<soap:Body>
<query xmlns="http://autotask.net/ATWS/v1_6/">
<sXML><![CDATA[<queryxml><entity>${entity}</entity><query><field>${field}<expression
op="${op}">${expression}</expression></field></query></queryxml>]]></sXML>
</query>
</soap:Body>
</soap:Envelope>`;
options = {
'method': 'POST',
'url': 'https://webservices15.autotask.net/ATServices/1.6/atws.asmx',
'headers': {
'Content-Type': 'text/xml',
'Authorization': 'Auth Goes Here'
},
body: xml
};
return new Promise((resolve, reject) => { // return a promise so that you can handle it when you invoke `query`
request(options, function (error, response) {
if (error) {
reject(new Error(error)); // reject instead of throwing, handle with `catch`
return;
}
text = response.body;
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
xmlResult = xmlDoc.getElementsByTagName(`${elementToParse}`)[0].childNodes[0].nodeValue;
resolve(xmlResult); // resolve instead of returning, handle with `then`
});
})
}
query('Ticket', 'TicketNumber', 'BeginsWith', 'T2019', 'id')
.then(xmlResult => console.log(xmlResult))
.catch(err => /* do something with that `new Error(error)` you rejected in `query`*/);