Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Node If Statement Not Firing Correctly

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.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!