-2

I have a api call which I am calling every 5 seconds. After each interval I want to store especially Marks from the api call into an js object/array so that I can pass that to a function to draw a graph. I have declared a var marks = [] and tried to assign data from api to marks, but when I am doing console.log(marks); outside the interval loop it says undefined.

The json data looks like this

{
  "PersonIds": [
    "001",
    "002"
  ],
  "Subjects": [
    {
      "Name": "Maths",
      "Marks": 90
    },
    {
      "Name": "Science",
      "Marks": 70
    },
  ]
}

$(document).ready(function (){

  //Start the graph
  marksGraph();
  var marks = []; //Contains marks
  var names = [];
  var rg, // Storesstartdate and end date range
        startDt, // Stores start Date
        endDt; // Stores end Date

    //Calling the Range API
    d3.json('url', function(error,data) {
      if(rg != null || rg!= undefined || !error){
        rg = data;
        startDt = new Date(data.startDt);
        endDt = new Date(data.endDt);

        //Set Count to zero
        var count = 0;
        var interval = setInterval(function process(){   
        if (startDt > fromDt) {
        clearInterval(interval);
        return 
        }
        console.log("I am coming inside Interval");
        var startDate = startDt.toISOString(); 

        var endDate = new Date(startDt.setMinutes(startDt.getMinutes() + 10)).toISOString();


        //Calling the API
        d3.json("url", function(error,datum) {
    if(datum != null){
            names = datum.name;
            marks = datum.marks;
          }
        });
        count++;
      },2500);
      console.log(marks);  
    }
  });
  console.log(names);
  console.log(marks);
hhhh4
  • 43
  • 1
  • 9
  • 1
    You need to declare your array outside of the interval – m0meni Jun 05 '16 at 23:45
  • 1
    Please show us the code, not just data. We have no idea why it is undefined unless you show us how it works – charlietfl Jun 05 '16 at 23:53
  • Wild guess is that it is a matter of timing. But, there's no way we can know without you showing the relevant code. As such, this question is unanswerable as is (other than wild guesses) and will be closed. – jfriend00 Jun 06 '16 at 00:03
  • I have added the code. Please take a look it ! – hhhh4 Jun 06 '16 at 00:25
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – forgivenson Jun 06 '16 at 00:28
  • @forgivenson Can you help me whats wrong with my code ? – hhhh4 Jun 06 '16 at 00:45
  • You are making an asynchronous call, and before it returns, the `console.log(marks)` line runs, so `marks` hasn't been set yet. There is a great answer in the question I linked that explains it all in detail. – forgivenson Jun 06 '16 at 00:53
  • @forgivenson In that case how can I store my response into a variable, the thread explains why it does that but how do I store my data into a varaible to pass it to a different function ? – hhhh4 Jun 06 '16 at 01:29

1 Answers1

1

Such problems usually occur because of the asynchronous nature of API calls. Can you post your source code of when you are making calls to the API? I am guessing you are getting a value of undefined because the non-blocking API call hasn't returned any value yet.

prithajnath
  • 2,000
  • 14
  • 17