Tedious not returning results to calling module

76 views
Skip to first unread message

andrew.w...@gmail.com

unread,
Aug 11, 2016, 8:40:32 PM8/11/16
to tedious
Hi I am new to both tedious and to Java Script. Not sure if my problem is that I don't understand callbacks well enough or ?? Anyway here is my code.  Connections worked in my implementation of the tedious example. 



I highlighted the code that is calling the function that is not working. Also where it fails.

Many thanks in advance for help.

// index.js

var server = require('./server');
var router = require('./router');
var requestHandlers = require('./requestHandlers');

//console.log('Here I am ');
var handle = {}
handle['/'] = requestHandlers.start;
handle['/start'] = requestHandlers.start;
server.start(router.route, handle);




// Server.js

var http = require('http');
var url = require('url');

function start(route, handle) {
     function onRequest(request, response) {
          var pathname = url.parse(request.url).pathname;
          console.log('request for   ' + pathname + 'recevied.');
               route(handle, pathname, response, request);
     }
http.createServer(onRequest).listen(8888);
console.log('Server has started...');
}
exports.start = start;


// Router.js

function route(handle,pathname, response, request) {
     console.log('About to route request for   ' + pathname);
     
if (typeof handle[pathname] === 'function') {
      handle[pathname](response, request);
} else {
    console.log('No request found for ' + pathname);
    response.writeHead(404, { 'Content-Type': 'text/plain' });
    response.write('404 Not Found');
    response.end();
     }
}
exports.route = route;


// RequestHandler.js



var querystring = require('querystring'),
     fs = require('fs'),
     clients = require('./getClients');

     
function start(response, request) {
     console.log('Request handler start was called');
     // call the tedious stuff here
     var values = clients.getdata(request, response);  // calling function that is failing.
     
     
    
     var body = "<html" +
          "<head>" +
          "<meta http-equiv='Content-Type' content = 'text/html; " +
          "charset=UTF-8' />" +
          "</head>" +
          "<body>" +

          "<form action='/upload' enctype='multipart/formdata' + method='post'>"  +
          "</form>" +
          "</body>" +
          "</html>";
     
          response.writeHead(200, { 'Content-Type': 'text/html' });
          response.write(body);
          response.end()
    
}



exports.start = start;


// getclient.js



var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
exports.getdata = function (req, res) {
     var config = {
          server: 'sbhg-sqlv02',
          userName: '*****',
          password: '****',
          options: {
               instanceName: '****'
          }
     };
     var sqlstring = "select evolv_cs.dbo.formatname(people.last_name, people.first_name, people.middle_name) as client " +
      " from  evolv_cs.dbo.people  join evolv_cs.dbo.event_log " +
      "on event_log.people_id = people.people_id " +
      " and event_log.event_definition_id = 'BA7EF74D-142D-4884-981A-6153755DFBE9' " + // program enrollment
      " and event_log.program_providing_service = '1D955DBF-529C-42D3-A2DA-C77B2036F642' " +   // Central Star PHF - Adolescent
      "  where   event_log.end_date is null ";
     var sqlconnection = new Connection(config);
     console.log('Req');
     console.log(Request.query);
     var cb = Request.query['callback'];  this throws error Reques.query is undefined. 
     var retval = '';
     console.log('WEF');
     sqlconnection.on('connect', function (err) {
          var request;
          request = new Request(sqlstring, function (err, rowCount, rows) {
               if (err) {
                    console.log('Error');
                    sqlconnection.close();
               } else {
                    var clients = [];
                    rows.forEach(function (columns) {
                         var rowdata = new Object();
                         columns.forEach(function (column) {
                              rowdata[column.metadata.colName] = column.value;
                         });
                         console.log(rowdata);
                         rowarray.push(rowdata);
                    })
                    sqlconnection.close();
                    res.contentType('application/json');
                    retval = JSON.stringify(rowarray);
                  /*  if (cb) {
                         retval = cb + '(' + retval + ');'
                    }    */
                    res.write(retval);
                    res.end();
               }
          });
          sqlconnection.execSql(request);    
     });
}




Reply all
Reply to author
Forward
0 new messages