0

I have a project in Node JS in which I want to export the data contained in the database in Mongo DB in a CSV file through a button in the view (index.ejs). I am using mongoose for the connection to the database and to export the data to the CSV I am trying to use json-2-csv.

In the button I have added a url to be able to call that url through the button and that the json-2-csv function responds to that url but I don't know how to do it or if it is the best way.

This is my app.js:

const fs = require('fs');
const json2csv = require("json2csv").Parser;
const userModel = require('./models/users');
const express = require("express");
const app = express();

app.get('/export/csv', async (req, res) => {
  await userModel.find((err, data) => {
      if (err) throw err;
      const json2csvParser = new json2csv({ header: true });
      const csvData = json2csvParser.parse(data);
      fs.writeFile("users.csv", csvData, function(error) {
          if (error) throw error;
          console.log("Write to bezkoder_mongodb_fs.csv successfully!");
      });
      
  });
});

This is the button:

<form action="/export/csv" mehotd="GET">
    <button id="export-csv">Export CSV</button>
</form>
sharp
  • 7
  • 4
  • First, you need to have a DB connection established and need to have data in the DB. The way you did is fine so where are you stuck can you explain more? – kedar sedai May 30 '21 at 02:55
  • @kedarsedai I do not know where to put the function if in the app.js or in the controller nor do I know how to import the connection to the database. And also, what I'm trying to do is call it from the button but I can't do it either, I don't know how to add to that function that responds to that url – sharp May 30 '21 at 09:08

1 Answers1

2

You can achieve all these things in your single file app.js file. We need to have json2csv module because this module has the parser class so that we can use parse() method to get the CSV format data as String. Here lean options tell mongoose to skip instantiating a full Mongoose document and just give you the Plain Old JavaScript Object POJO. And also I have used username and password as documents so change it accordingly.

const path = require('path');
const ejs = require('ejs');
const fs = require('fs');
const express = require('express');
//You need to have some documents into your DB first 
const Collection = require('your/Modal Path');
const Json2csvParser = require("json2csv").Parser;
const app = express();
const port = process.env.PORT || 3000;

//Templating Engine Ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

//Middleware
app.use(express.urlencoded({
    extended: true
}));
app.use(express.json());

//MONGO DB CONNECTION 
const url = 'mongodb://localhost:27017/users';
mongoose.connect(url, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => {
        console.log('Successfully Established Connection with MongoDB')
    }).catch(err => {
        console.log('Failed to Establish Connection with MongoDB with Error: ' + err);
        process.exit();
    });

app.get('/export/csv', async (req, res) => {
    await Collection.find({}).lean().exec((err, data) => {
        if (err) throw err;
        const csvFields = ['_id', 'username', 'password']
        console.log(csvFields);
        const json2csvParser = new Json2csvParser({
            csvFields
        });
        const csvData = json2csvParser.parse(data);
        fs.writeFile("bezkoder_mongodb_fs.csv", csvData, function(error) {
            if (error) throw error;
            console.log("Write to bezkoder_mongodb_fs.csv successfully!");
        });
        res.send('File downloaded Successfully')
    });
});

//HOME route
app.get('/', (req, res) => {
    res.render('home.ejs');
});

//listening to the PORT Number 
app.listen(port, console.log(`Server is running at ${port}`));

So, this is how your app.js file will look like. And also create a home.ejs file inside views directory like views/home.ejs. and add the below code:

<form action="/export/csv" mehotd="GET">
    <button id="export-csv">Export CSV</button>
</form>
kedar sedai
  • 1,687
  • 3
  • 16
  • 25
  • I have several problems: a csv is downloaded in the root folder of the project but it does not contain the data of the database, it only contains the data type and the name of the field. Another problem I have is that when downloading it the process does not end I do not know how to finish it. I have updated the question with the current code. – sharp May 30 '21 at 17:12
  • Ok now if it works, great thanks. Another question I have now, is it possible to change the destination folder, for example, that the file is downloaded in the Downloads folder and that the downloaded symbol appears in the browser? – sharp May 31 '21 at 06:51
  • yes, of course you can change the `destination` folder like `fs.writeFile("C:/folder/bezkoder_mongodb_fs.csv", csvData, function(err) { ..... ` – kedar sedai May 31 '21 at 06:56
  • Ok thanks it works great. Could you help me in another question that I have open 'Handle errors in Node JS in the data model'. What I am trying is to show errors in the input form, for example, if in the age field I insert a String that will show me an error and the form is restarted. I've searched but can't find anything on this with Node JS and I'm pretty new. – sharp May 31 '21 at 07:07
  • do you mean if the user inputs age that is of string which needs to be in integer then you need to show an error like (age should be in number )? – kedar sedai May 31 '21 at 07:18
  • Yes, I want to know if that is possible to show the error and restart the form. – sharp May 31 '21 at 07:24
  • you can achieve this from the `mongoose validation` later showing message in the `ejs` https://mongoosejs.com/docs/validation.html – kedar sedai May 31 '21 at 12:17