Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

export mongo to csv node

const json2csv = require('json2csv').parse;

//For unique file name
const dateTime = new Date().toISOString().slice(-24).replace(/D/g, 
'').slice(0, 14); 

const filePath = path.join(__dirname, "../../../", "public", "exports", "csv-" + dateTime + ".csv");

let csv; 

const student = await req.db.collection('Student').find({}).toArray();

// Logging student
// [{id:1,name:"John",country:"USA"},{id:1,name:"Ronny",country:"Germany"}]

const fields = ['id','name','country'];

 try {
        csv = json2csv(booking_info, {fields});
    } catch (err) {
        return res.status(500).json({err});
    }

 fs.writeFile(filePath, csv, function (err) {
        if (err) {
            return res.json(err).status(500);
        }
        else {
            setTimeout(function () {
                fs.unlink(filePath, function (err) { // delete this file after 30 seconds
                if (err) {
                    console.error(err);
                }
                console.log('File has been Deleted');
            });

        }, 30000);
            res.download(filePath);
        }
    })
Comment

PREVIOUS NEXT
Code Example
Javascript :: electron vue printer 
Javascript :: react native layout animation android 
Javascript :: Material-ui Adb icon 
Javascript :: javascript select letter in string 
Javascript :: node express config file json 
Javascript :: javascript Check Map Elements 
Javascript :: javascript undefined 
Javascript :: to array javascript 
Javascript :: javascript eingabe in inputfielder übernehmen 
Javascript :: tagged templates 
Javascript :: what is express static 
Javascript :: fibbanacci sequence 
Javascript :: react setstate concat string 
Javascript :: filter array js 
Javascript :: how to find remainder in javascript 
Javascript :: node http request 
Javascript :: ionic not compiling with proxy 
Javascript :: js example 
Javascript :: updatedAt mongoose stop 
Javascript :: math.round 
Javascript :: js variable to string 
Javascript :: google script get sheet size 
Javascript :: convert Component Did mount into useEffect 
Javascript :: slimscroll javascript 
Javascript :: convert json string to byte array java 
Javascript :: create owl component react js 
Javascript :: javascript array find case insensitive 
Javascript :: get all database react native 
Javascript :: destructuring props in react 
Javascript :: react native spinkit 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =