Reputation: 161
I'm working to a project for exporting csv file using this export-to-csv library but it returns an error "ExportToCsv is not a constructor" . I'm not sure what might be the problem. Hoping you could help me.
const ExportToCsv = require('export-to-csv');
module.exports.ExportToCsv = async (req, res) => {
try {
var data = [
{
name: 'Test 1',
age: 13,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
{
name: 'Test 2',
age: 11,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
{
name: 'Test 4',
age: 10,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: true,
title: 'My Awesome CSV',
useTextFile: false,
useBom: true,
useKeysAsHeaders: true,
};
const csvExporter = new ExportToCsv(options);
csvExporter.generateCsv(data);
}catch(e) {
console.log(e);
}
}
Upvotes: 1
Views: 1404
Reputation: 2361
Use either of these statements for importing the module:
const ExportToCsv = require('export-to-csv').ExportToCsv
import { ExportToCsv } from 'export-to-csv';
The problem with your code is that you're importing and referring to the entire module, what you need is a specific function in the module. The error correctly points out that your variable ExportToCsv
is not a constructor, which is true as it refers to the entire exports object from the module that you're importing. The constructor function that you need to use is a property on the object that's exported from the module so you have to point to that specific property.
Upvotes: 1