fs.createReadStream之后未执行代码块

时间:2019-07-25 09:30:01

标签: javascript arrays node.js fs

我正在尝试读取上载的.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}

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

1 个答案:

答案 0 :(得分:1)

有一些执行顺序上的误解。

  

在通过fs.createReadStream读取文件之后,似乎未执行我的console.log。

实际上,console.log是在以fs.createReadStream()开始流之后立即执行的。读取流已启动,仅当事件on()data由流本身触发时才执行两个end,因此您应该在任何{ {1}}。

此外,'sample output'是在流结束之前打印的,因此它是零(理论上可以是某个值,但实际上从来都不是真正的值)。

为您的代码添加一些注释以使其更加清晰:

'CSV file successfully processed'

执行顺序概述:

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

解决方案:您应将点3 4 5放入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);
});