0

I have this function which I use to extract a string value from an xml and then convert to a float. It is as follows

function stringtoFloat() {
  var parser = new xml2js.Parser();

  fs.readFile('C:\\Temp\\tasks\\acis\\110-1100.sat\\110-1100.sat.response.xml', function (err, data) {
    parser.parseString(data, function (err, result) {
      var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;

      var fileTime = timeString.substr(13, 20);
      var filetimeVal = parseFloat(fileTime);

      return filetimeVal;       
    });    
  });
};

Then I run this function 5 times to sum up the values from each of those runs

function funcTotal() {
  var sum = 0;

  for (itr = 1; itr <= 5; itr++) {
    var numFunc = stringtoFloat(); //store the output from the function in a variable
    sum = sum + numFunc; //Get the sum of all 5 variables
  }

  console.log(sum);
  return sum;
}

funcTotal();

When I run this I get the result as 'NaN' Why is this? and how do I resolve this?

I have updated the code by replacing fs.readFile with fs.readFileSync in the following manner.

function parseTime(){

    var parser = new xml2js.Parser();

    var data = fs.readFileSync('C:\\Temp\\tasks\\acis\\110-1100.sat\\110-1100.sat.response.xml', {encoding:'utf8'});

    parser.parseString(data, function (err, result) {

    var timeString = result.Message.Response[0].Events[0].MessageReportEvent[8].$.Message;
    var fileTime = timeString.substr(13,20);
    var filetimeVal = parseFloat(fileTime);

    console.log(filetimeVal);
    return filetimeVal;

    });

    };

Apparently half of the problem is resolved. It seems the funcTotal() runs after stringtoFloat now, unlike before. But still I get NaN. Can you please help?

TMA
  • 111
  • 1
  • 2
  • 9
  • I see few syntax errors – Tushar Nov 19 '15 at 05:52
  • 1
    I think it was premature to close this as a duplicate. It's not immediately obvious to novice JS developers how the linked question relates to this one. – Dancrumb Nov 19 '15 at 05:55
  • 1
    @Dancrumb `readFile` and `parseString` are asynchronous from where the value is returned from the function. So, value will always be `undefined` and `0 + undefined` ==> `NaN`. – Tushar Nov 19 '15 at 05:57
  • 1
    @Tushar, yeah... i get that, but it's not obvious to someone who is new to JS and closing this question as a duplicate doesn't really serve the goal of providing guidance to SO users. – Dancrumb Nov 19 '15 at 05:58
  • 1
    It's not actually possible to [`return` the result when using asynchronous functions](http://stackoverflow.com/q/14220321), like `fs.readFile()`. You'll have to revise `stringtoFloat()` and `funcTotal()` to use callbacks, [`Promise`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), or similar flow control. With Promises, you can use [`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) to know when all 5 rounds of `stringtoFloat()` have completed to calculate and display the `sum`. – Jonathan Lonowski Nov 19 '15 at 06:29
  • How do I add a call back to this? – TMA Nov 19 '15 at 21:55
  • What if I use 'fs.readFileSync' instead of fs.readFile? – TMA Nov 19 '15 at 23:00

0 Answers0