0

In console it is showing the obj but in very next line when I'm trying to access it shows an error: cannot read property 'date' of undefined. I'm not getting what is the mistake.

I tried following: console.log(obj[9].date)

Please go through success : function(obj)

<script type="text/javascript">
  $("#date-picker").flatpickr({
    defaultDate : new Date()
  });

  console.log($("#date-picker").val());

  $("#date-picker").on('change',function(){
      console.log($("#date-picker").val());
      var select_date = $("#date-picker").val();
      console.log("select_date : ",select_date);

      $.ajax({
      type: "POST",
      url: "http://127.0.0.1:8000/attendance/",
      dataType: 'json',
      async: true,
      data: {
        "selected" : '1',  
        "select_date" : select_date,
        csrfmiddlewaretoken : '{{ csrf_token }}'
      },
      success: function(obj){
        console.log("post alerts");
        console.log("success");
        console.log(obj);
        console.log(obj[9].date);

      failure: function(data){
        console.log("failure");
        console.log(data);
      },
    });
  })

 </script>

console.log(JSON.stringify(obj)) shows:

{
    "obj": [{
        "student_id": 1,
        "date": "2019-06-20",
        "check_in": "2019-06-19T11:27:52.692Z",
        "check_out": "2019-06-19T11:27:52.693Z",
        "tutor_time": "2019-06-19T11:27:52.693Z",
        "intention": "",
        "absent_present": "",
        "reasonOfAbsence": ""
    }, {
        "student_id": 2,
        "date": "2019-06-20",
        "check_in": "2019-06-19T11:27:52.762Z",
        "check_out": "2019-06-19T11:27:52.762Z",
        "tutor_time": "2019-06-19T11:27:52.762Z",
        "intention": "",
        "absent_present": "",
        "reasonOfAbsence": ""
    }],
    ...
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Can you show what `console.log(JSON.stringify(obj))` looks like? – Barmar Jun 19 '19 at 16:31
  • See https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Barmar Jun 19 '19 at 16:32
  • {"obj":[{"student_id":1,"date":"2019-06-20","check_in":"2019-06-19T11:27:52.692Z","check_out":"2019-06-19T11:27:52.693Z","tutor_time":"2019-06-19T11:27:52.693Z","intention":"","absent_present":"","reasonOfAbsence":""},{"student_id":2,"date":"2019-06-20","check_in":"2019-06-19T11:27:52.762Z","check_out":"2019-06-19T11:27:52.762Z","tutor_time":"2019-06-19T11:27:52.762Z","intention":"","absent_present":"","reasonOfAbsence":""}] – Rahul Dewangan Jun 19 '19 at 16:38
  • When I'm trying to access it shows undefined. – Rahul Dewangan Jun 19 '19 at 16:48

2 Answers2

0

Change this: console.log(obj[9].date); to this console.log(obj.date[9]);

0

The object has an obj property that contains the array of students. So you need to use

console.log(obj.obj[9].date);

Also, you need to ensure that obj.obj.length is at least 10 in order to access index 9, so use

if (obj.obj.length >= 10) {
  console.log(obj.obj[9].date);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612