如何解决readstream文件中的每个错误?

时间:2017-06-13 18:14:20

标签: javascript node.js

当我在forEach之后调用aysncFiles()方法时,我看到以下错误,当我们使用异步库时如何解决readstream问题?

基本上只想从客户端中挑选出与开始日期和结束日期匹配的文件。

ctrl.js

function readFile(searchObj,callback) {
    searchStr = searchObj.searchTxt;
    searchStartDate = searchObj.searchStartDate;
    searchEndDate = searchObj.searchEndDate;
    var filesData = {};
    var readStream;
   if (searchObj.searchEnv === 'st') {
        stDirectory.readDirectory(function(files){
        filesData.logFiles = files;
        var sortedFiles = [];
        filesData.searchEnv = searchObj.searchEnv;
        filesData.logFiles.forEach(function(file){
                 // move this in front of the loop
             var fileDate = new Date( file.fileDate ).getTime();
             var searchStartDate = new Date( searchStartDate ).getTime(); // add missing
             var searchEndDate = new Date( searchEndDate ).getTime(); //     `var`s
             if (fileDate - searchStartDate > 0 && searchEndDate - fileDate > 0) {
                 console.log('File Date',file);
                 sortedFiles.push(file);
             }
        });
         filesData.logFiles = sortedFiles;
         console.log(filesData.logFiles);
         asyncFiles(filesData); // Now being called
     });
    } 

    // loop through each file
  function asyncFiles(filesData) {
  async.eachSeries(filesData.logFiles, function (logfile,done) {
        // read file
        var results = [];
        readStream = fs.createReadStream('./logs/' + filesData.searchEnv  + '/'+ logfile.filename,'utf8')
         readStream.pipe(split())
          .on('data', function (line) {

              done();
    }
    , function (err) {
        if (err) {
            console.log('error', err);
        }
       readStream.on('end',function(){
        callback(matches);
       });
       matches = [];
    });
  }

}

错误

 readStream.on('end',function(){
                 ^

TypeError: Cannot read property 'on' of undefined

0 个答案:

没有答案