title: Node JS Soap Server(node-soap) passing values from postgres callback
body: <p>I have a question I am using node-soap and pg-promise. I have a problem where when I make a request to the soap service the response is delayed, in other words I have to send the request twice (using soap ui) before the response comes back:</p>
  
  <p>Im wondering if I am not passing the response from the database call correctly to node soap call back:</p>
  
  <p>Here is the sample code from node-soap website:</p>
  
  <pre><code>var myService = {
      MyService: {
          MyPort: {
              MyFunction: function(args) {
                return {
                    name: args.name
                };
              },
  
              // This is how to define an asynchronous function.
              MyAsyncFunction: function(args, callback) {
                // do some work
                callback({
                    name: args.name
                });
              },
  
              // This is how to receive incoming headers
              HeadersAwareFunction: function(args, cb, headers) {
                return {
                    name: headers.Token
                };
              },
  
              // You can also inspect the original `req`
              reallyDeatailedFunction: function(args, cb, headers, req) {
                console.log('SOAP `reallyDeatailedFunction` request from  + req.connection.remoteAddress);
                return {
                    name: headers.Token
                };
              }
          }
      }
  };
  
  var xml = require('fs').readFileSync('myservice.wsdl', 'utf8'),
  server = http.createServer(function(request,response) {
    response.end("404: Not Found: " + request.url);
  });
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>                        var http = require('http');
                          var soap = require('soap');
                          global.results = '';
  
  
                          var promise = require('bluebird'); // 
                          var options = {
                          promiseLib: promise // switch to bluebird lib
                      };
  
                      // Database connection details;
                      var cn = {
                          host: '',
                          port: 5432,
                          database: '',
                          user: '',
                          password: ''
                      };
  
  
                         var pgp = require('pg-promise')(options);
  
                         var db = pgp(cn);
  
  
                         var cancelService = {
                          cancelService: {
                              Cancel_PortType: {
                                  getAgreement: function(args,callback,headers) {
  
  
                                      var values = {
                                              vin: args.vin
                                      };
  
                                      db.any("select contract_num as contract, " +
                                          "CASE WHEN sg_con_status='A' THEN 'Active ' " +
                                          "WHEN sg_con_status='X' THEN 'Expired' " +
                                          "WHEN sg_con_status='C' THEN 'Cancelled' " +
                                          "END AS agreementStatus, " +
                                          "tfs_product_type as productType, " +
                                          "vin, cust_first_name as customerFirstName,    cust_last_name as customerLastName," +
                                          "TO_CHAR(original_busdate,'YYYY-MM-DD') as purchaseDate,   TO_CHAR(expire_date,'YYYY-MM-DD') as expirationDate " +
                                          "from tfs_contract_summary, sg_con_m1 where sg_con_m1.sg_con_contract = tfs_contract_summary.contract_num " +
                                          " AND vin = ${vin}",values)
  
                                      .then(function(data) {
  
                                      //set value of DB return to global
                                           global.results = data;
  
                                          console.log("DATAAAAAAA:", data);
  
                                      })
  
  
                                      .catch(function(error) {
                                              console.log("ERROR:", error); // print the error;
                                          })
                                          .finally(function() {
  
                                              pgp.end(); //closing the connection pool.
  
                                          });
  
  
                                      //  }
  
  
  
                                      callback({
                                          contracts: global.results
                                      });
                                  }
                              }
                          }
                      }
                      var xml = require('fs').readFileSync('CancelService.wsdl', 'utf8'),
                          server = http.createServer(function(request, response) {
                              response.end("404: Not Found: " + request.url)
  
  
                          });
  
  
                      server.listen(8000);
                      soap.listen(server, '/wsdl', cancelService, xml);
  </code></pre>
  
  <p>All of my console.log() show the data coming back everytime I fire off a request in SOAPUI but the response does not come back until the 2 time I sent the request:</p>
  
  <p>For example</p>
  
  <p>Hit button once:
  vin = 12312364812736498</p>
  
  <p>I expect John doe info to return -
  <strong>console.log(John doe's info)</strong>
  <strong>SOAP RESPONSE = empty</strong></p>
  
  <p>Hit Button twice
  <strong>console.log(John doe's info)</strong>
  SOAP RESPONSE = John doe's info</p>
  
  <p>If I use the soap client sample code provided and issue a request:</p>
  
  <p>console.log works everytime:</p>
  
  <p>Do I have a scope issue where the global.results value has a race condition??? </p>
  
  <p>I'm pretty sure it's probably something wrong with my code as I am a newbie to Node JS etc...</p>
  
  <p>Am I passing to the callback of the soap-server incorrectly??</p>
  
  <pre><code>callback({
              contracts: global.results
           });
  </code></pre>
  
  <p>Any insight would be greatly appreciated.</p>