Error converting type String to Integer in NodeJS

0

In Nodejs I am trying to check the value of a variable of type String, but for some reason nothing is working. Example:

var aaa = "0";  // Eu recebo essa variável como string (não dá para mudar o tipo)

console.log(typeof(aaa));   // String
console.log(Number(aaa));   // NaN   ===> Não deveria ser número 0?
console.log(parseInt(aaa)); // NaN   ===> Não deveria ser número 0?

// Isso sempre dá FALSE
aaa = "1";
if (aaa)
    console.log('true');
else
    console.log('false');

// Isso sempre dá FALSE também
aaa = "1";
if (Number(aaa))
    console.log('true');
else
    console.log('false');

What am I missing?

REAL CODE:

  <head>
    <title>TESTE</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <link rel="stylesheet" href="/stylesheets/bootstrap.min.css">
    <script src="/socket.io/socket.io.js"> </script>
    <script>
        var socket = io.connect();

        function changeState(state) {
            if (state == 1) {
                socket.emit('changeState', '{"state":1}');
                document.getElementById("outputStatus").innerHTML = "Status: ON";
            } else {
                socket.emit('changeState', '{"state":0}');
                document.getElementById("outputStatus").innerHTML = "Status: OFF";
            }
        }


        socket.on('sensorsUpdate', function (data) {
            var reading = JSON.parse(data);
            document.getElementById("IO_opened").innerHTML = reading.IO_opened;
            document.getElementById("IO_closed").innerHTML = reading.IO_closed;

            if (reading.IO_opened) {
                console.log('ABRIR');
                document.getElementById("IO_opened").className = "exibe";
                document.getElementById("IO_closed").className = "n_exibe";
            } else if (reading.IO_closed) {
                console.log('FECHAR');
                document.getElementById("IO_opened").className = "n_exibe";
                document.getElementById("IO_closed").className = "exibe";
            } else {
                console.log('Open = ' + reading.IO_opened + ' Close = ' + reading.IO_closed);
                document.getElementById("IO_opened").className = "n_exibe";
                document.getElementById("IO_closed").className = "n_exibe";
            }
        });
    </script>
  </head>

Never enter these "if (reading.IO_"). It only enters the last "else". This javascript code is written in a separate file and is inserted directly into the page through a PUG / Jade view, through an include:

  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
    script(src='/socket.io/socket.io.js') 
    include ../public/javascripts/sensorStatus.js   //<<<<<====== Include do javascript
  body
    
asked by anonymous 22.07.2017 / 05:29

1 answer

0

I already found out where the error was.

// Eu estava fazendo isso ERRADO!!
var j = {
    "IO_opened": '"' + ioOp + '"',
    "IO_closed": '"' + ioCl + '"'
}

// CORRIGIDO: Agora funciona
var j = {
    "IO_opened": ioOp,
    "IO_closed": ioCl
}

// Esses valores são enviados para a página dessa forma e o JS trata por meio dos "ifs".
s.emit("sensorsUpdate", JSON.stringify(j));

The first way I was doing wrong and instead of IO_opened/IO_closed receive "0" or "1" , they received ""0"" or ""1"" , with two quotes, which resulted in Number(IO_opened) = NaN in the conversion there in JS.

This JSON was in another file .... It's a lot of information for a beginner in Nodejs and JS ... I'm very stuck to the standard of other languages. Thank you all!

    
22.07.2017 / 17:39