1

I am trying to pass data from CSV via Axios to Apexcharts and cannot figure out what am I doing wrong. I am working with this question but cannot make it work.

I need to get the CSV data into const lst that will hold the data outside of any function, so that I can then use it in parameters for the Apexchart.

No matter what way I try to do it, lst will not hold the data when console.log'd outside of the function.

What is wrong?

const lst = []; // need to fill this 
const populateData = (data) => {lst.push(data)} // from the other question but makes no difference
function csvJSON(csvStr){ 
      ...
      // this is necessary to format the CSV into a form that apexcharts understand
      populateData(x);
      populateData(y); // tried with .push directly too, none works 
      // if I console.log here, the data is there, but cannot return it
      // I thought the populateData handles the thing that axios returns Promise, but clearly it doesn't
     
 }
 function getCSV(endpoint){
       axios.get(endpoint).then( function(res){
          csvJSON(res.data);
       });
 }
 getCSV("https://example/data.csv");
     console.log(lst) // is empty
Phil
  • 157,677
  • 23
  • 242
  • 245
JMT
  • 11
  • 2
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Phil Sep 01 '22 at 23:17
  • @Phil Thank you very much good sir. I am finally starting to understand how this works. – JMT Sep 03 '22 at 15:05

0 Answers0