How to show callback response in browser

I want to make it simple. Please someone can help me. Any positive reply will be appreciated. My simple question is how to print the final array in the browser. I am trying to display it in a browser but cannot do this. Please help me with this. I am new to the system.

var http = require('http');
var mysql = require('mysql'); 
http.createServer(function(req, res) {

    var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'DB_USER',
      password : 'DB_PASS',
      database : 'DB_NAME',
    });

    connection.connect();
    var resp = [];
    async.parallel([
      function(callback){
        setTimeout(function(){
          soap.createClient(url, function(err, client) {
              client.setSecurity(new WSSecurity('USER_NAME', 'PASSWORD'));
              client.SubmitMessage(strContent1, function(err, result) {
                  resp.push(result.message.payload.unit_of_business_results['premium_details']);
              });
          });
        }, 200);
      },
      function(callback){
          setTimeout(function(){
              soap.createClient(url, function(err, client) {
                client.setSecurity(new WSSecurity('USER_NAME', 'PASSWORD'));
                client.SubmitMessage(strContent2, function(err, result) {
                    resp.push(result.message.payload.unit_of_business_results['premium_details']);
                });

              });
          }, 100);
      },
      function(callback){
          setTimeout(function(){
              soap.createClient(url, function(err, client) {
                client.setSecurity(new WSSecurity('USER_NAME', 'PASSWORD'));
                client.SubmitMessage(strContent3, function(err, result) {
                    resp.push(result.message.payload.unit_of_business_results['premium_details']);
                });
              });
          }, 100);
      }
    ],
    // optional callback
    function(err, results){
        // the results array will equal ['one','two'] even though
        // the second function had a shorter timeout.
    });

    res.writeHead(200);
    res.end(JSON.stringify(resp));
}).listen(8020);
+4
source share
1 answer

Move the response to productQuery.on('end')

docs: https://github.com/felixge/node-mysql/#streaming-query-rows

var http = require('http');
var mysql = require('mysql'); 
http.createServer(function(req, res) {

    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'DB_USER',
        password : 'DB_PASS',
        database : 'DB_NAME',
    });

    connection.connect();

    var prod = {};
    var productsFinished = false;
    var productQuery = connection.query('SELECT * FROM tbl_product 1') ;
    productQuery.on('result', function (prod_rows) {
        prod[prod_rows.product_id] = prod_rows;
    }); 

    var employer = {};
    var employerFinished = false;
    var employerQuery = connection.query('SELECT * FROM tbl_employer 1') ;
    employerQuery.on('result', function (empl_rows) {
        employer[empl_rows.employer_id] = empl_rows;
    }); 

    var sendIfFinished = function () {
        if(productsFinished && employerFinished) {
            res.writeHead(200);
            res.end(JSON.stringify(prod)); // or whatever you want to send.
        }
    }

    productQuery.on('end', function () {
        productsFinished = true;
        sendIfFinished();
    });

    employerQuery.on('end', function () {
        employerFinished = true;
        sendIfFinished();
    });


}).listen(8020);
0
source

All Articles