1

How do you parse csv files that have different header names? I was trying to do it somewhat like this in meteor method:

parseUpload(data){
check(...)
for(let i=0;i<data.length;i++){
  let item = data[i]
  let thing = {
    name: item.name || item.itemname || item.something,
    category: item.category
  }
  items.insert(thing);
}

but it was always taking the first param (item.name) and as it was empty, the method couldn't be executed. How do i do OR statement to bind name to doc.namesArray so uploaded file can use aliases for column names?

also, i have noticed that when you make spreadsheet not starting from 1A, it creates empty lines/rows in csv document. papaparse still expects first column to be "name" and everything stops working again (untill i recreate csv file so it starts with actual values).

Mikkel
  • 7,693
  • 3
  • 17
  • 31

1 Answers1

0

If you try to paste this code into the console window in your browser:

item={name:' ',itemname:"itemname",something:"something"}
item.name || item.itemname || item.something

You'll get this:

" "

Similarly with this:

item={name:'',itemname:"itemname",something:"something"}
item.name || item.itemname || item.something

You get

"itemname"

So if item.name is an empty string, or missing, then you get item.itemname returned

So the code is ok, your data must not be as you expect. Try putting a console.log(item) in and see what you get, or set a breakpoint and inspect it.

Mikkel
  • 7,693
  • 3
  • 17
  • 31
  • yeah but when i upload file that has only one of those expected headers, it responses with "sanitized and reported to the client as: name is required [400]. – Marcin Buglewicz Feb 02 '17 at 12:12