1

I am having a hard time wrapping my head around how i can manipulate the stack in JS Hopefully this exercise will help me..

So im trying write a function that makes a soap xml call and parses the data. Then returns the parsed data when called.

I can get it to console.log the result when i call the function, but i can not get it to return the result so i can store it in a variable ect.. It returns undefined.

Code Below:

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

      };

    request(options, function (error, response) { 
      if (error) throw new Error(error);
       text =  response.body;
       parser =  new DOMParser();
       xmlDoc =  parser.parseFromString(text,"text/xml");
       xmlResult = xmlDoc.getElementsByTagName(`${elementToParse}`)[0].childNodes[0].nodeValue;
       **console.log(xmlResult)**
      });
}

query('Ticket','TicketNumber','BeginsWith','T2019','id') 
// returns result if i log the data in REQUEST
//returns undefined otherwise
  • 1
    You cannot return the result. Because node.js networking is asynchronous and non-blocking, your function will return BEFORE you have your data. Thus, you cannot return it directly from your function. Instead, you must use a callback or a promise to communicate back the data when it eventually arrives. See the question yours has been marked a duplicate of for a full explanation and examples. – jfriend00 Jan 24 '20 at 17:33

1 Answers1

1

You should use Promises to react to future events and deal with asynchronous code execution in 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`*/);
gabriel.hayes
  • 2,267
  • 12
  • 15