0

I am new to Node js and express. I trying to query my Azure database to get the list of patients(MR number). I am getting the list but I think the request.on() runs as an asynchronous function so first, it will render the HTML page without any list and after executing the function Node Js exit executing.

Code:

var express = require('express');
var router = express.Router();
//For database
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

function mrquery(connection) {
  request = new Request(
    "SELECT DISTINCT MRNO FROM Transaction_Record",
    function(err, rowCount, rows) {
      console.log(rowCount + ' row(s) returned');
      process.exit();
    }
  );
  request.on('row', function(columns) {
    columns.forEach(function(column) {
      console.log(column.value);
    });
  });
  connection.execSql(request);
}

/* GET home page. */
router.get('/', async function(req, res, next) {
  var connection = req.app.get('connection');
  var isconnected = req.app.get('isconnected');
  if (isconnected) {
    mrquery(connection)
    res.render('index', {
      title: 'Express'
    });
  } else {
    console.log('Not Connected');
    res.render('error', {
      error: 'error'
    });
  }

});
module.exports = router; 

Thanks in advance.

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
GauthamGAjith
  • 367
  • 1
  • 4
  • 19
  • You first should take a look at e.g. [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321), to find a solution how to get a result from an async function. – t.niese Jul 31 '18 at 10:17
  • `[...] Node Js exit executing.[...]` does it shown an error on exit and if so what error? – t.niese Jul 31 '18 at 10:18

0 Answers0