【问题标题】:Code block not being executed after fs.createReadStream在 fs.createReadStream 之后没有执行代码块
【发布时间】:2019-12-03 12:55:42
【问题描述】:

我正在尝试读取上传的.csv 文件并将其存储在一个数组中,以便将数据批量插入到我的数据库中,我使用csv-parser 包和fs 用于文件流。

router.post('/fileupload', function(request, response, next){
var bulk_emp_data = [];
if(request.files){
  var sampleFile = request.files.filename;
  console.log(sampleFile.name);
  sampleFile.mv('C:\\Users\\QPS-AUDRICK\\Desktop\\QBOS Timekeeping\\qbos\\public\\temp\\'+sampleFile.name, function(err){
    if(err){
      console.log('Error moving ' + err);
    }else{
      fs.createReadStream('C:\\Users\\QPS-AUDRICK\\Desktop\\QBOS Timekeeping\\qbos\\public\\temp\\'+sampleFile.name)
        .pipe(csv())
        .on('data', (data) => results.push(data))
        .on('end', () => {
          console.log(results);
          bulk_emp_data = results;
          console.log('CSV file successfully processed');
          console.log(bulk_emp_data.length);
        });
        console.log('sample output');
    }
  });
}

var data = {
    success: 1,
    msg: 'Successfully parsed the sample file.',
    data: bulk_emp_data.length
};

response.json(data);
});

通过fs.createReadStream 读取文件后,我的console.log 似乎没有被执行。这是终端的最后两行。

CSV file successfully processed
63

此外,bulk_emp_data 变量似乎已被清空。这是response的结果

{"success":1,"msg":"Successfully parsed the sample file.","data":0}

请注意,终端上的最后一行是 63,而它应该是 console.log(),而在 response 上,bulk_emp_data.length 是 0

【问题讨论】:

    标签: javascript arrays node.js fs


    【解决方案1】:

    存在一些执行顺序的误解。

    通过 fs.createReadStream 读取文件后,我的 console.log 似乎没有被执行。

    实际上console.log 是在使用fs.createReadStream() 启动流后立即执行的。读取流启动,两个on() 仅在流本身触发事件dataend 时执行,因此您应该在任何'CSV file successfully processed' 之前看到'sample output'

    bulk_emp_data.length 也是在流结束之前打印出来的,所以它是零(理论上它可以是某个值,但实际上永远不是真正的值)。

    在您的代码上添加一些注释以更清晰:

    router.post('/fileupload', function (request, response, next) {
      var bulk_emp_data = [];
      if (request.files) {
        var sampleFile = request.files.filename;
        console.log(sampleFile.name);
    
        // sync operation - the execution "waits" here
        sampleFile.mv('C:\\Users\\QPS-AUDRICK\\Desktop\\QBOS Timekeeping\\qbos\\public\\temp\\' + sampleFile.name, function (err) {
          if (err) {
            console.log('Error moving ' + err);
          } else {
    
            // async operation
            // start a stream and attach some event listener
            fs.createReadStream('C:\\Users\\QPS-AUDRICK\\Desktop\\QBOS Timekeeping\\qbos\\public\\temp\\' + sampleFile.name)
              .pipe(csv())
              // executed when the stream fires the 'data' event
              .on('data', (data) => results.push(data))
              // executed when the stream fires the 'end' event
              .on('end', () => {
                console.log(results);
                bulk_emp_data = results;
                console.log('CSV file successfully processed');
                console.log(bulk_emp_data.length);
              });
    
            // executed after starting the stream (and pratically before any event that the stream can fire)
            console.log('sample output');
          }
        });
      }
    
      // executed after "console.log('sample output')"  (and pratically before any event that the stream can fire)
      var data = {
        success: 1,
        msg: 'Successfully parsed the sample file.',
        data: bulk_emp_data.length
      };
    
      // executed pratically before any event that the stream can fire
      response.json(data);
    });
    

    执行顺序回顾:

    1. sampleFile.mv()
    2. fs.createReadStream()
    3. console.log('样本输出')
    4. var 数据 = { ... }
    5. response.json(数据)
    6. fs.createReadStream().on('data') X 次
    7. fs.createReadStream().on('end') 1 次

    解决方案:您应该将点 3 4 5 放入 .on('end') 处理程序中,例如

    .on('end', () => {
      console.log(results);
      bulk_emp_data = results;
      console.log('CSV file successfully processed');
      console.log(bulk_emp_data.length);
    
      console.log('sample output');
    
      var data = {
        success: 1,
        msg: 'Successfully parsed the sample file.',
        data: bulk_emp_data.length
      };
    
      response.json(data);
    });
    

    【讨论】:

    • 似乎我对文件流的了解不足确实是这里的问题。谢谢!
    • 发生了,幸运的是解决方案并不复杂!尝试新的实现,如果它工作正常,也请把答案标记为接受:)
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多