繁体   English   中英

为什么fast-csv不将数据存储在NodeJS中?

[英]Why fast-csv doesn't store the data in NodeJS ?

您好,我只想在节点中存储CSV列以操纵数组中的数据,而我正试图读取并存储它,但是它不起作用是怎么回事? 这是我的代码

let stream = fs.createReadStream("Supliers.csv");

let csvStream = csv({headers: true})
.on("data", function(data){
     /*data.forEach(x => {
      console.log(x)
     })*/

     Final.push(data)
})
.on("end", function(){
     console.log(Final);//[THE COMPLETE DATA TOTALLY OK]
});

stream.pipe(csvStream);

console.log(Final)// []

我不明白:(帮助

根据您的代码,我添加了注释。

let stream = fs.createReadStream("Supliers.csv");

let csvStream = csv({headers: true}).on("data", function(data){
     // This is inside a function that gets slightly later, in the 
     // next tick of the event loop.  I.e. it's called after
     // the 'console.log(Final)' statement.
     Final.push(data)
})
.on("end", function(){
     // You can do your processing in this function
     console.log(Final);
});

stream.pipe(csvStream);

// This gets called before the '.on("data"...' function.
console.log(Final)

// To prove that this works (***but don't use this in your code***)
// the following should work too.
setTimeout(() => console.log(Final), 100);

我建议您阅读有关Node.js事件循环的内容: https : //nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM