wataru wataru - 2 months ago 7
Javascript Question

Uncaught TypeError: Cannot read property of null

Hello im having trouble of fixing this error

Uncaught TypeError: Cannot read property '2' of null
. This is what i want to do, if i click to the day without event the
#myModal
will show but if i click the day with event the
#delete
modal will show. But my problem is when im clicking the day without event im getting an error of
Uncaught TypeError: Cannot read property '2' of null
.

enter image description here
here's the ajax

dayClick: function(date, allDay, jsEvent, view) {
var datee = date.format();
$.ajax({
url:'getrecords.php',
method:'POST',
dataType: 'json',
data:{
"filter": 1,
"start": datee

},
success:function(data){
var date = data[2];

if(date == datee){
$('#delete').modal('show');
}
else{
$('#myModal').modal('show');
}

}
});


here's the php

if(isset($_POST['filter']))
{
$start = $_POST['start'];
$select = mysqli_query($con,"SELECT * FROM appointment WHERE start = '$start'");
$check_user = mysqli_num_rows($select);
$array = mysqli_fetch_row($select);
echo json_encode($array);
}

Answer

This is most likely the result of the data being passed into your success callback being null. Accessing it like it is an array will then yield the type error you are seeing. Without know too much about your app, maybe try changing your logic to:

//your previous code then handle success like:           

        success:function(data){
            if (data === null) {
               $('#myModal').modal('show');
            } else {
               $('#delete').modal('show');
            }            
         }