Node.js — transform file buffer to array of object from html file input (json, csv)
Published in
1 min readMar 30, 2019
After I upload the json and csv file from the html file input <input type=”file”>, and pass it to Node.js. I want to insert those records into MongoDB, I can get the file from req.file. However, how do I transform the req.file to the correct format (a list of object)?
Here is the solution, you will need to install csvtojson module.
json: JSON.parse(req.file.buffer.toString());
csv : await csv().fromString(req.file.buffer.toString());
The list will be an array of json object, and you can use insertMany method to insert the records to MongoDB through mongoose.
Reference: