Closed joaonice closed 6 years ago
Looks like we need to tweak that message, but basically it shows up because your data has no items.
https://github.com/zemirco/json2csv/blob/master/lib/JSON2CSVParser.js#L43
Is there a reason you'd want a CSV without any data?
In my code, in some situations, my empty state data is an empty array (fetching some data from database). As a temporary fix, I'm checking if the array is empty and transform it in an array with an empty object. In that case, I just want a CSV with the headers.
Give v4.0.3 a try.
@knownasilya thank you but I'm getting the same error because the first element of data
is not an object.
if (!this.opts.fields && processedData.length === 0 || typeof processedData[0] !== 'object')
Ah, yes I'll fix that.
Try v4.0.4
Thank you!
Try v4.0.4
not working, still getting the same error
Try v4.5.4
with 4.5.4 and 4.0.4 both versions
// CSV Stuff
public csvFileName = 'taluka.csv';
public fieldsData = ['id','name'];
getCSVDownloadLink() {
return this.generateCSVDownloadLink({
filename: this.csvFileName,
data: this.json2csv_data,
fields: ['id','name'],
includeEmptyRows: true
});
}
public generateCSVDownloadLink(options: { filename: string, data: any[], fields: string[], includeEmptyRows?: boolean}): SafeUrl {
const opts = {output: [options.filename, ['id','name'], options.includeEmptyRows]};
const csv = json2csv.parse(options.data, opts);
return this.domSanitizer.bypassSecurityTrustUrl('data:text/csv,' + encodeURIComponent(csv));
}
We are using it in this fashion, on load t works as options.includeEmptyRows
is present but fails when button click is initiated with the same error on each time the button is clicked.
Is our fields field wrongly used here ? @knownasilya
const opts = {output: [options.filename, ['id','name'], options.includeEmptyRows]};
seems wrong to me.
I'd have expected:
const opts = {
output: options.filename,
options.fields,
options.includeEmptyRows
};
@juanjoDiaz thanks for picking this up, been crazy over here with a move and a 2 month old 😆
I am getting this error in node JS
Make sure your options are correct, like @juanjoDiaz mentioned in https://github.com/zemirco/json2csv/issues/280#issuecomment-573155448
Try v4.5.4
I installed 5.0.7 version. But getting this error. Should I move to v4.5.4?
Try v4.5.4
I installed 5.0.7 version. But getting this error. Should I move to v4.5.4?
I've moved to that version but still getting the error. Any suggestion?
@MehediHasan0009 did you try the https://www.npmjs.com/package/json2csv/v/6.0.0-alpha.1 release?
We have the issue also in the latest release.. has this not been fixed yet?
This repo was abandoned. You should move to https://github.com/juanjoDiaz/json2csv which is the continuation of this project. There you can find the latests versions where this was fixed long time ago.
json2csv version: 4.0.2
Code:
Getting the error:
Data should not be empty or the "fields" option should be included
Fields are configured and I'm still getting the error.