gpt4 book ai didi

javascript - fs.createReadStream 之后代码块未执行

转载 作者:行者123 更新时间:2023-12-02 23:15:04 27 4
gpt4 key购买 nike

我正在尝试读取上传的 .csv 文件并将其存储在数组中,以便将数据批量插入到我的数据库中,我正在使用 csv-parser package 和用于文件流的 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 变量似乎已被清空。这是响应

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

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

最佳答案

存在一些执行顺序误解。

After reading the file thru fs.createReadStream, it seems that my console.log is not being executed.

实际上,console.log 在使用 fs.createReadStream() 启动流后立即执行。读取流启动,只有当流本身触发事件 dataend 时才会执行两个 on() ,因此您应该在任何“成功处理 CSV 文件” 之前查看“示例输出”

此外,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);
});

关于javascript - fs.createReadStream 之后代码块未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com