It treats each element in the array as an array, so '456' is treated as an array with the elements 4,5,6.
A strange workaround I did was to loop through the array and put each element inside its own array, so it would be:
var newArray = [];
for( var i = 0; i < tempArray.length; i++){
var arr = [];
arr.push(tempArray[i]);
newArray.push(arr);
}
And then write the "newArray".
But otherwise if you write row by row, you can put each element inside a bracket, like:
var fast_csv = fastcsv.createWriteStream();
var writeStream = fs.createWriteStream("outputfile.csv");
fast_csv.pipe(writeStream);
for(var i = 0; i < tempArray.length; i++){
fast_csv.write( [ tempArray[i] ] ); //each element inside bracket
}
fast_csv.end();
Answer from Galivan on Stack Overflow
» npm install fast-csv
Videos
It treats each element in the array as an array, so '456' is treated as an array with the elements 4,5,6.
A strange workaround I did was to loop through the array and put each element inside its own array, so it would be:
var newArray = [];
for( var i = 0; i < tempArray.length; i++){
var arr = [];
arr.push(tempArray[i]);
newArray.push(arr);
}
And then write the "newArray".
But otherwise if you write row by row, you can put each element inside a bracket, like:
var fast_csv = fastcsv.createWriteStream();
var writeStream = fs.createWriteStream("outputfile.csv");
fast_csv.pipe(writeStream);
for(var i = 0; i < tempArray.length; i++){
fast_csv.write( [ tempArray[i] ] ); //each element inside bracket
}
fast_csv.end();
Change: tempArray.push(data[1]); to: tempArray.push([data[1]]);
writeToPath takes an array of arrays.
Docs: https://www.npmjs.com/package/fast-csv
You can't turn an asynchronous function into a synchronous one.
You could, however, wrap that eventful reading code into a promise, which you can then use in a promise/async context, like so:
const fs = require("fs");
const csv = require("@fast-csv/parse");
function readCsv(path, options, rowProcessor) {
return new Promise((resolve, reject) => {
const data = [];
csv
.parseFile(path, options)
.on("error", reject)
.on("data", (row) => {
const obj = rowProcessor(row);
if (obj) data.push(obj);
})
.on("end", () => {
resolve(data);
});
});
}
async function doThings() {
const data = await readCsv(
"./downloads/aggiornamento.csv",
{ skipRows: 2 },
(row) => ({ item_id: row[2], qty: row[20] }),
);
// use data in API...
}
Callback functions can be wrapped with promise.
const fs = require("fs");
const csv = require("@fast-csv/parse");
let data = []
let readCSVSynchronously = async () = {
return new Promise(reject, resolve => {
const results = [];
csv
.parseFile("./downloads/aggiornamento.csv", { skipRows: 2 })
.on("error", (error) => console.error(error)
return reject(error);
)
.on("data", (row) => {
// Do your logic
results.push(row);
})
.on("end", function () {
resolve(results)
});
});
}
const totalResults = await readCSVSynchronously();
Well, I know that this question has been asked a long back but just now I got to work with CSV file for creating API with node js. Being a typical programmer I googled "Reading from a file with fast-csv and writing into an array" well something like this but till date, there isn't any proper response for the question hence I decided to answer this.
Well on is async function and hence execution will be paused in main flow and will be resumed only after nonasync function gets executed.
var queryParameter = ()=> new Promise( resolve =>{
let returnLit = []
csv.fromPath("<fileName>", {headers : true})
.on('data',(data)=>{
returnLit.push(data[<header name>].trim())
})
.on('end',()=>{
resolve(returnLit)
})
})
var mainList = [];
queryParameter().then((res)=>mainList = res)
If you want to validate something pass argument into queryParameter() and uses the argument in validate method.
The "on data" callback is asynchronous, and the commands that follow the callback will run before the callback finishes. This is why the code does not work, and this reasoning has been pointed out by others who have posted answers and comments.
As for a good way to accomplish the task, I have found that using the "on end" callback is a good fit; since the intention here is to "do something" with the whole data, after the file has been read completely.
var dataArr = [];
csv.fromPath("datas.csv", {headers: true})
.on("data", data => {
dataArr.push(data);
})
.on("end", () => {
console.log(dataArr.length);
// > 4187
});
» npm install @fast-csv/parse