Reputation: 6263
A little background.
I am receiving a message from my server that comes in the form of <Buffer 00 00 00>
I using the Node string_decoder function to turn this into a utf8 encoded string:
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
global.message = null;
client.on('data', function(chunk) {
console.log(`New Message ${decoder.write(chunk)}`);
message = decoder.write(chunk);
})
When running, this logs out New Message OK
which is correct
In another function, I am then looping until the global variable is equal to OK, however I am running into some problems:
console.log(`Global vari ${message}`);
if(message != "OK"){
setTimeout(check, 1000);
} else {
return cb(true);
}
The above logs out Global vari OK
however the if statement fails. I have done a typeof check on message, and it outputs string as expected.
I'm stumped as to what is going on here, but I cannot figure it out for the life of me.
Upvotes: 0
Views: 52
Reputation: 67505
Try to trim before comparaison to make sure there no extra spaces :
if(sanitized.trim() != "OK"){
setTimeout(check, 1000);
} else {
return cb(true);
}
Hope this helps.
Upvotes: 1
Reputation: 111306
Are you sure it doesn't contain any BOM character or any other invisible data that makes it not equal to OK?
You can try removing everything but the letters, like:
let sanitized = message.replace(/\W/g, '');
console.log(`Global vari ${sanitized}`);
if(sanitized != "OK"){
setTimeout(check, 1000);
} else {
return cb(true);
}
and see what it prints now.
You can see these answers about BOM characters and the problems they cause:
Upvotes: 0