I am doing a project where I am using a Tessel to read a text file on a server and perform a function based upon the value. I am fairly new to node.js, and have managed to get most of the items running, but I am having an issue getting the comparison to work.
I know it is reading the value (which will either be a "1" or "0") fine because when I incorporate a console.log(//variable) in the code, the console shows the value.
The basic code I have been playing with is:
var http = require('http');
setInterval(function() {
var options = {
host: 'www.users.wfleitz.com',
path: '/fleitzwc/example.txt'
};
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str)
if (str == "1"){
console.log("It reads fine ")
}
});
}
http.request(options, callback).end();
}, 1000);
I have tried moving the "if" statement around, and also wording the "if" statement:
if (str.toString == "1"){
console.log("It reads fine ")
}
Could someone tell me if it is a placement, or syntax issue? Or if I am off base all together? I was thinking it may be that an issue where although the file should only have a "1" or "0" in it, I have had instances where I needed to trim the file contents to ensure I was only getting that one value, but I can't find a way to do it in node.js.
Thank you in advance, WFleitz
" 1"etc. and you can try doingif ( str == 1 )or usingtrim()or loggingstr.lengthetc.