0

I am a complete noob to node and javascript in general.

I have managed to piece the below script together using a few tutorials:

//HTTP Server
var app = require('express')();
var http = require('http').Server(app);

//Socket.IO
var io = require('socket.io')(http);

//MSSQL Server
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;  
var Types = require('tedious').Types;  
var config = {userName: '********',  password: '********',server: 'localhost'};  
var connection = new Connection(config); 

//Global
var connectedclients = 0;
var candidatedatenew = "NULL";
var candidatedateold = "NULL";

//Serve Page
app.get('/', function(req, res){
res.sendFile(__dirname + '/Socket.html');
});

//HTTP Server Listen
http.listen(3532, function(){
  console.log('HTTP Server Listening On Port 3532');
});

//Socket Send And Receive
io.on('connection', function(socket){
  connectedclients++;
  console.log(connectedclients);
  socket.on('disconnect', function(){
    connectedclients--;
    console.log(connectedclients);
  });  
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);
    io.emit('chat message');
  });
});

//On MSSQL Server Connect 
connection.on('connect', function(err) {   
    console.log("Connected To MSSQL");  
executeStatement();
    });

//SQL Query
    function executeStatement() {  
        request = new Request("SELECT MAX([datetime]) [datetime] FROM [******].[**].[********];", function(err) {  
        if (err) {  
            console.log(err);}  
        });  
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                candidatedatenew = "NULL";
              } else {  
                candidatedatenew = column.value;  
              }
            });  
        });   
        connection.execSql(request);  
    }

//Refresh Query
function sqlRefresh () {
  executeStatement();
  if (candidatedatenew !== candidatedateold) {
  console.log(candidatedatenew);
  console.log(candidatedateold);
  io.emit('refresh');
  candidatedateold = candidatedatenew;
  }
}

setInterval(sqlRefresh, 10000);

The trouble is, my code under the sqlRefresh in the if statement seems to fire regardless of whether the values are the same or different.

It is probably something really simple but, as stated, I am a complete newbie to this.

  • 1
    the if statement is getting executed before executeStatement() returns and changes candidatedatenew. executeStatement should return a promise – Cruiser Apr 27 '17 at 13:05
  • What is the type of the column values? Have you checked what the data you're getting from the database looks like? – Pedro Castilho Apr 27 '17 at 13:05
  • 1
    `executeStatement` changes `candidatedatenew` asyncrhonouosly – Jaromanda X Apr 27 '17 at 13:05
  • @Cruiser - or at least accept a callback argument for "old school" asynchronous code handling – Jaromanda X Apr 27 '17 at 13:06
  • executeStatement change the value later than if block execution and before console.log execution and hence you running into error. – binariedMe Apr 27 '17 at 13:06
  • @JaromandaX true, that would work too. – Cruiser Apr 27 '17 at 13:07
  • Don't get me wrong @Cruiser - your suggestion is exactly what I would suggest, as Promises are far easier to work with (once you get your head around them) – Jaromanda X Apr 27 '17 at 13:10
  • Thanks for your comments - After spending a while looking at promises, I am more lost than I was before! Google ahoy! – Daniel Hitchcock Apr 27 '17 at 13:51
  • So I added this: `request.on('doneProc', function() { if (candidatedatenew !== candidatedateold) { console.log('Refreshing Viz'); io.emit('refresh'); candidatedateold = candidatedatenew; } });` This is the callback but, it still doesn't work. Am I even close?? – Daniel Hitchcock Apr 27 '17 at 19:01

0 Answers0