无法访问nodejs代码中的值

时间:2016-11-07 08:56:43

标签: node.js line-by-line

我的nodejs函数中有以下代码。即使IF条件为真,我也没有为stringResponse变量更新值。

var fs = require('fs'), 
                byline = require('byline');
            var stream = fs.createReadStream('Solutionbank.csv');
            var stringResponse = "We found a similar issue.  ";
            stream = byline.createStream(stream);
            stream.on('data', function(line) {
              var csvLine = line.toString('utf8');
                  csvLine = csvLine.toLowerCase().trim();

              var msgText = subject.toLowerCase().trim();
              //console.log("email block",msgText);
              if(csvLine.indexOf(msgText) > -1) {
                var arr = line.toString('utf8').split(",");

                stringResponse = stringResponse + "Issue ID"+arr[0]+",Subject : "+arr[1]+", Resolution Comment:"+arr[2];
                console.log(stringResponse);

                }
            });

            console.log("stringResponse from email",stringResponse);

1 个答案:

答案 0 :(得分:1)

行:

  

console.log(" stringResponse from email",stringResponse);

是outsite on(' data')回调函数,在设置stringResponse变量之前调用此行。把它放在回调函数的末尾(内部)。或者在流末尾调用的另一个回调函数中:

stream.on('data', function(line) {
  ...
  console.log("stringResponse from email",stringResponse); // show each append 
});

  stream.on('end', function() {
  ...
  console.log("stringResponse from email",stringResponse); // show each append 
});