I'm using fast-csv
npm package to parse incoming csv file and catching event data
and displaying row
is absolutely OK. I got what I want here.
What I've done:
fs.createReadStream(req.file.path)
.pipe(csv.parse({ ignoreEmpty: true, trim: true }))
.on("error", (error) => console.error(error))
.on("data", (row) => console.log(row))
.on("end", (rowCount) => console.log(rowCount));
What I got in
data
event:
['Data Entry Form']
['no','name','email','phone','address']
['1','John','[email protected]','09111111111','abc']
['2','Mary','[email protected]','09111111111','abc']
What I need:
let data = [];
fs.createReadStream(req.file.path)
.pipe(csv.parse({ ignoreEmpty: true, trim: true }))
.on("error", (error) => console.error(error))
.on("data", (row) => data.push(_.compact(row)))
.on("end", (rowCount) => console.log(rowCount));
return res.status(201).send({
status: "success",
data,
});
NOT OK:
data
is empty and I got no error.
Any Suggestions? Thanks.
You should respond only when the read stream is ended, like this:
let data = [];
fs.createReadStream(req.file.path)
.pipe(csv.parse({ ignoreEmpty: true, trim: true }))
.on("error", (error) => console.error(error))
.on("data", (row) => data.push(_.compact(row)))
.on("end", (rowCount) => {
console.log(rowCount);
res.status(201).send({
status: "success",
data,
});
});