0

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

4
  • 1
    If the console logs what you say it does, it should work. I'm guessing the console really logs something with spaces, like " 1" etc. and you can try doing if ( str == 1 ) or using trim() or logging str.length etc. Commented Apr 29, 2015 at 15:07
  • parseInt() would work too, since the possible values are 0 and 1. Commented Apr 29, 2015 at 15:20
  • Thank you all. @Kevin B, the parseInt() did the trick. Commented Apr 29, 2015 at 16:55
  • Actually, after playing with it a little more, I found that the parseInt() wasn't working. It didn't matter what the .txt value was, it always compared it as a "1". Not to say a syntax error on my part wasn't stopping it, but I did get it going using the trim() method. After researching it a little more, and figuring out the proper way to implement the trim(), it works. I updated my answer below. Commented Apr 29, 2015 at 17:34

2 Answers 2

4

You need to call the toString() function like so:

if (str.toString() == "1") {
  // Note ------^^ parens actually call the function.
  console.log("It reads fine ")
}
Sign up to request clarification or add additional context in comments.

2 Comments

The chunks are being concatenated to a string, so it shouldn't be a buffer, I think ?
He's talking about the "i tried this..." snippet. which of course won't solve the problem
0

I ended up using the trim() method like this:

  response.on('end', function () {
    var strTrim = str.trim();
       console.log(strTrim)

     if (strTrim == "1"){
     console.log("It reads fine")
}
  }     

Thank you all again for your help.

2 Comments

Might just be a typo, but strInt = 1 is an assignment, not a logical comparison.
@Tony, I updated my comments above. That was probably the syntax error on my part. See what happens when I get all giddy about something? Typos galore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.