I am creating an application that allows users to upload a CSV. The CSV is just a comma separated file.
On the frontend, I send the file to my backend with a PUT request:
Reactjs | fileupload_frontend.js
sendFile = () => {
const data = new FormData();
data.append('file', this.myCSV_file);
axios.post('/parse-csv', data)
.then(response => console.log('File sent to server for parsing')
.catch(error => console.log(error);
}
The above code successfully sends the uploaded CSV (info) to my server, which is listening at the /parse-csv
endpoint.
I am using the csv-parser
npm package to help with this.
Nodejs | fileupload_backend.js
const csv = require('csv-parser');
const fs = require('fs');
const results = [];
app.post('/parse-csv', (request, response) => {
fs.createReadStream(request.files.file.data) { // this is a buffer on the req obj
.pipe(csv())
.on('data', results.push())
.on('end', (results) => {
console.log(results);
}
}
}
For reference, the request object from the frontend to the backend looks like:
Request from upload { name: 'data.csv',
data: <Buffer 22 89 56 24 5y 86 k9 22 . ... >,
encoding: '7bit',
truncated: false,
mimetype: 'text/csv',
md5: [Function: md5],
mv: [Function: mv] }
It is error-ing out on each request with a message that seems to be related to my header rows (first row in the csv file), with a message that reads:
Error: ENOENT: no such file or directory, open 'my,header,row,is,referenced,here'
...then the data is shown below.
Do I have to save the CSV file in some directory, then parse it first? I am just looking for a way to parse the CSV into json, so I can insert each row as an entry into my database.