I have taken the code that @jonatandor35 posted last October to use Wix fetch to retrieve csv content and load it into Wix datasource. It works fine (see code below) except for two issues:
- because the csv is quote delimited, all the headers and values are inserted into the Wix datasource surrounded in quotes. I used replace() function with the headers to eliminate the double-quotes before inserting (because I know there are no other quotes or commas in the headers besides the delimiters; but the values may have commas in the text as well as double quotes, therefore I can’t just strip all the double-quotes out.
Here is the fetch(url) return for my simple test csv as displayed in console log using JSON.stringify():
//fetch result: “"FirstName","LastName","Narrative"\n"kyle","collins","The first ‘Collins’ narrative."\n"kyle","collins","Second Collins narrative."\n"jenny","collins","First Jenny Collins narrative."”
So the first question is how to eliminate the delimiter quotes when the data are inserted into the Wix datasource fields?
My attempted solution is to use a specialized NPM package for csv-to-json conversion (e.g., csvjson or csvtojson)–to see if it clears out the delimiter characters, but also to handle complex csv values, because J.D.'s code only works for the simplest values (no internal commas). But that led to my second issue:
- I get an error in trying to use the npm csv converters–error is that csvjson() is not a “function” (and same trying a different converter). This is most probably due to my fumbling of the grammar. Thank you so much for any pointers about errors in this code:
import {fetch} from ‘wix-fetch’ ;
import {csvjson} from ‘csvjson’ ;
// I also tried: const csvjson = require ( ‘csvjson’ ); (which shows error that “require” not defined, but still runs)
export function parseCSV(myURL) {
const sheetURL = myURL ;
fetch(sheetURL)
.then(r => r.text()).then(r => {
console.log( “fetch result: " + JSON.stringify(r) );
/*fetch result: “"FirstName","LastName","Narrative"\n"kyle","collins","The first ‘Collins’ narrative."\n"kyle","collins","Second Collins narrative."\n"jenny","collins","First Jenny Collins narrative."” */
let data=JSON.stringify(r);
csvjson(data, {quote: '”’ }) // THIS LINE THROWS ERROR: CSVJSON NOT A FUNCTION and stops.
.then(res=> {
console.log(res);
})
})
Thanks again (!) for any suggestions on how I get npm csv converter to run and process the fetch() result.