Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda