0

I'm trying to feed a csv file into Chart.js using Papa Parse. I tried to define the result from Papa Parse as a global array, but I found two following examples I'm confused with.

<!doctype html>
<html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.3/papaparse.min.js>
</script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js>
</script>
<script>

var helloworld=Papa.parse("1,2,3,4,5").data;

document.write(JSON.stringify(helloworld));
</script>
</html>

This code works well, so I tried to tune it to feed a csv file.

<!doctype html>
<html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.3/papaparse.min.js>
</script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js>
</script>
<script>

var helloworld=Papa.parse("csv.csv",{download:true}).data;

document.write(JSON.stringify(helloworld));
</script>
</html>

I thought I did the same thing as the csv.csv file contains just the five numbers separated by commas, but the code printed nothing. Which part do I need to correct? Thank you.

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
Junyong Kim
  • 279
  • 1
  • 2
  • 10
  • Doesn't papaparse return a promise when downloading a file? You cannot synchronously access the results from an asynchronous download. – Bergi Feb 20 '19 at 18:09
  • As the [documentation](https://www.papaparse.com/docs#remote-files) already says for `Papa.parse(url, { download: true `: _"Doesn't return anything. Results are provided asynchronously to a callback function."_ – Andreas Feb 20 '19 at 18:09
  • Thanks for these comments. Though have used HTML before, I began JavaScript just a few days ago, so didn't think about the sync/async issue yet. It seems I can use `Papa.parse("csv.csv",{download:true,complete:function(results){document.write(JSON.stringify(results.data));}});`, but couldn't define the results.data array as a global array inside, i.e. `Papa.parse("csv.csv",{download:true,complete:function(results){var helloworld=results.data;}});`. Should I learn the callback function first to do this? – Junyong Kim Feb 20 '19 at 21:05
  • [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-I-return-the-response-from-an-asynchronous-call) – Andreas Feb 21 '19 at 13:35

0 Answers0