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