letter Q
letter Q

Reputation: 15405

Can't load .csv file on server in D3.js code

So the following code for loading the .csv file is not working:

<script type="text/javascript">
  var final_news_events = [];
  d3.csv("final_news_events.csv", function(data, error) {
    // You're function goes in here.
    console.log("data = ", data);
    final_news_events = data;
  });
</script>

I have the above code hosted on github one line 104 in file new_event_list.html

I also pushed this code to heroku and have a live website here: https://neweventdetection.herokuapp.com/

When you open the console for the website it shows data = null

From the line of code console.log("data = ", data); in the file new_event_list.html line 108

Upvotes: 1

Views: 1007

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599661

You have the arguments in the wrong order in function(data, error).

Swap them and things will work a lot better:

  d3.csv("final_news_events.csv", function(error, data) {

But keep in mind that the loading is still an asynchronous operation. So instead of assigning the data to a more globally scoped variable (final_news_events), do what all the samples for d3 do: act on the data inside the callback function: https://github.com/mbostock/d3/wiki/CSV

Upvotes: 2

Related Questions