Tuesday 23 May 2017

How do i test on implementing converting CSV to JSON in node.js?

So basically I am tasked to implement a import function for a web-based inventory management app. It uses node.js and MongoDB and HTML5 for runtime environment and database respectively. I am supposed to be able to import excel sheets in csv format to JSON format in the sense of selecting a file for importing to the MongoDB. I have managed to find a question in regards to this but am stuck as to how to test it out. I have finished in terms of my api for basic functions like update and insert etc by testing on postman however I am not started in my html code for the webpage. This function is for adding records easily for the user so that they do not need to insert the records one by one. This is what I have found in converting CSV to JSON using the csvtojson module. I have also found mongoimportbut it is executed on mongo shell through

mongoimport --db myDb --collection myCollection --type csv --headerline --file /path/to/myfile.csv and so I did not use it.

//Converter Class
var Converter = require("csvtojson").Converter;

var fs=require("fs"); 
//CSV File Path or CSV String or Readable Stream Object
var csvFileName="./myCSVFile";

//new converter instance
var csvConverter=new Converter({});

//end_parsed will be emitted once parsing finished
csvConverter.on("end_parsed",function(jsonObj){
console.log(jsonObj); //here is your result json object
});

//read from file
fs.createReadStream(csvFileName).pipe(csvConverter);

Do I include this code as another api or put it as a separate code by itself?

How do I test the import function out? Do I have to create my html code first to test it out?

Do forgive me if my question sounds like an opinion-based question as I am currently unable to find something on this. If anyone has read about something like this do link it in the comments or answers or if there is a better solution for importing large amount of records from excel to MongoDB through node.js, do tell me. Any help is appreciated! Thank you.

Both code on mongoimport and csvtojsonare from here.

How to convert CSV to JSON in Node.js

how to import excel file (XLSX) to mongoDB

Executing mongoimport inside code with Javascript/Node.js This is something I read up on as well on mongoimport and from what I understand mongoimport is for importing data that has been exported from other applications or databases.



via Ong Kong Tat

No comments:

Post a Comment