0

I have gone through link: variable scope in d3 javascript as my data was not getting loaded in the variable at first.
I tried the following code:

let train;
// d3.csv("training.csv", function(data){train = data;});
function myFunc(data) {
    train = data;
}

d3.csv("training.csv", function (data) {
    var csv = data;
    myFunc(csv);
});

Still I am getting the variable as undefined when I tried to log it through console.log()
See the following image:
output image

Please let me know what I can do. Have I been missing something? Please suggest me what I can do to avoid this issue.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • Where in the above code is your `console.log`? Because `d3.csv` is *async* it must be in the `d3.csv` callback or some function called from it. – Mark Dec 17 '18 at 13:05
  • But I am storing the csv data in the variable `train` and that variable I am fetching in the anther file where I am assigning it to different process. In that file I tried to log the `train' variable. even if I put this whole code in the same file still I get the same issue. – Jaffer Wilson Dec 17 '18 at 13:30
  • Same file, different files doesn't matter. All processing on that variable needs to stem from the `d3.csv` callback. It'll only reliably be available there... – Mark Dec 17 '18 at 13:56

0 Answers0