Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么fast csv没有';是否将数据存储在NodeJS中?_Node.js_File_Csv - Fatal编程技术网

Node.js 为什么fast csv没有';是否将数据存储在NodeJS中?

Node.js 为什么fast csv没有';是否将数据存储在NodeJS中?,node.js,file,csv,Node.js,File,Csv,您好,我只想在节点中存储一列CSV,以操作数组中的数据,我正在尝试读取并存储它,但它不起作用。发生了什么?这是我的密码 let stream = fs.createReadStream("Supliers.csv"); let csvStream = csv({headers: true}) .on("data", function(data){ /*data.forEach(x => { console.log(x) })*/ Final.p

您好,我只想在节点中存储一列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)// []

我不明白:(HELP

基于您的代码,我添加了注释

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事件循环:

如何存储数据以在其他函数中使用它?感谢您的回答,这取决于您想对它做什么。您可以将必要的代码放入
.on(“end”…
函数中。这是处理数据的好地方。我将尝试@psiphi75谢谢