最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - String compare with node.js - Stack Overflow

programmeradmin0浏览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 parison 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',
  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

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 parison 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.',
  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

Share Improve this question asked Apr 29, 2015 at 15:05 Will FleitzWill Fleitz 131 gold badge1 silver badge4 bronze badges 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. – adeneo Commented Apr 29, 2015 at 15:07
  • parseInt() would work too, since the possible values are 0 and 1. – Kevin B Commented Apr 29, 2015 at 15:20
  • Thank you all. @Kevin B, the parseInt() did the trick. – Will Fleitz 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 pared 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. – Will Fleitz Commented Apr 29, 2015 at 17:34
Add a ment  | 

2 Answers 2

Reset to default 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 ")
}

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.

发布评论

评论列表(0)

  1. 暂无评论