Closed selected-pixel-jameson closed 3 years ago
same problem here!.. any solutions yet?
installed 6.0.0-alpha.. problem persists
So the fields for the parser only take an array of strings at this time. Looking to see if that's a mistake or not.
Honestly it should work, the tests show it working: https://github.com/zemirco/json2csv/blob/master/test/JSON2CSVParser.js#L294
Do either of you see anything different you are doing? @nahumdam esp you since this is v6
I just ran you example on json2csv 5.0.6
var { Parser } = require("json2csv")
const fields = [
{
label: 'Name',
value: 'name'
},
{
label: 'Email',
value: 'email'
},
{
label: 'Practitioner',
value: 'practitioner'
},
{
label: 'Event',
value: 'referrer'
},
{
label: 'Topic',
value: 'topic'
},
{
label: 'Code',
value: 'code'
},
{
label: 'Offer ID',
value: 'htoOfferId'
}
]
let data = [{
name: "Alannak",
email: "xxxxxxx",
practitioner: 0,
referrer: "mentalwellness.xxxxxx.com",
topic: "Mental Health",
code: "MNWL20",
htoOfferId: null,
}]
let json2csv = new Parser({ fields })
let csv = json2csv.parse(data)
console.log(csv)
the result was exactly as expected
"Name","Email","Practitioner","Event","Topic","Code","Offer ID"
"Alannak","xxxxxxx",0,"mentalwellness.xxxxxx.com","Mental Health","MNWL20",
So not sure what you are doing differently...
I tried your code and it worked, thanks.
For some reason, I just renamed my variable to "fields" and suddenly it worked. If I call it any other way, labels do not work... which makes no sense, but I'll research more on it later
let json2csv = new Parser({ fields: someOtherName })
Should work
you are right, it does work! thanks
I think this is our answer @selected-pixel-jameson add "fields:" before the name of your variable on the new Parser declaration
Thanks! Might want to update the docs.
For some reason my field labels are not being rendered. It just continues to use the object values in the data set that I'm sending through. Sorry if this is something small that I'm missing. I tried to search for past issues, but didn't find anything that seemed relevant.
5.0.6 NodeJS 10.x
Fields
This is what is returned. Please note this is just sample data.
"name","email","practitioner","referrer","topic","code","htoOfferId" "Gentjan","xxxxxxxx",0,"diydetoxsummit.byhealthmeans.com","Detox","DIY21", "Laura","xxxxxxx",0,"adrenalsolutions.byhealthmeans.com",,"ADRL21", "Gnanagirija","xxxxxxxxxx",0,"adrenalsolutions.byhealthmeans.com",,"ADRL21",