skip to Main Content

I created a SOAP server in Node.js

const soap = require('soap');
const http = require('http');
const XMLWriter = require('xml-writer');

const service = {
   ImpDispatcherService: {
      ImpDispatcher: {
         performTest: function (args) {
            const xw = new XMLWriter(true);
            xw.startDocument();
            xw.startElement('result');
            queryDatabase(args.parameter1, xw).then(success => {
               console.log(success);
               xw.endDocument();
               console.log(xw.toString()); // -> prints the full XML-File
            });             
            return { testResult: xw.toString() };
         }
      }
   }
};

var server = http.createServer((request, response) => {
   response.end('404: Not Found: ' + request.url);
});

// Create the SOAP server
const xml = require('fs').readFileSync(`${__dirname}/ImpDispatcherService.wsdl`, 'utf8');
var server = http.createServer((request, response) => {
   response.end('404: Not Found: ' + request.url);
});

server.listen(8000, 'localhost');
soap.listen(server, '/imp', service, xml, () => {
   logger.info('Server initialized');
});

async function queryDatabase(pars) {
   const oracledb = require('oracledb');
   const connection = await oracledb.getConnection(db);
   const result = await connection.execute(`BEGIN :ret := getResult(${pars}); END;`, {ret: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR } });
   let row;
   while (row = await result.outBinds.ret.getRow()) {
      xw.writeElement('tag', row.blabla);
   }

   return true;
}

When I make a SOAP request from client, then it returns only empty <result/> element but not the elements I added inside queryDatabase().

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution:

    const service = {
       ImpDispatcherService: {
          ImpDispatcher: {
             performTest: queryDatabase
          }
       }
    }
    
    async function queryDatabase(args) {
       const xw = new XMLWriter(true);
       xw.startDocument();
       xw.startElement('result');
       await queryDatabase(args.parameter1, xw);
       xw.endDocument();
       
       return { testResult: xw.toString() };
    }
    

    However, I did not manage it to define the function inside the service object. But this is mainly cosmetic.


  2. performTest: function (args) {
      queryDatabase(args.parameter1).then(result => {
        return result;
      });
    }
    

    You don’t have a return statement in performTest, so it’s implicitly returning undefined, instead of a promise. Also, .then(result => result) has no effect. The promise created by queryDatabase will already resolve to result.

    Try this instead:

    performTest: function (args) {
      return queryDatabase(args.parameter1)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search