Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kylie's avatar
Level 1

Uncaught TypeError: Cannot convert undefined or null to object while using FullCalendar in Laravel

MyReservations.blade.php

        <div class="modal-body">
            <!-- Room -->
            <select class="form-control" aria-label="Default select example"  id="room_id" >
                <option selected>Select Room</option>
                @foreach($room as $items)
                    <option value="{{$items->id}}">{{$items->id}}</option>
                @endforeach

            </select>
            <br>
            <!--  Title -->


            <div class="input-group">
                <span class="input-group-text"> Title : </span>
                <input class="form-control" aria-label="Title"  id="title"></input>

            </div>
            <br>
            <!-- Objectif -->
            <div class="input-group">
                <span class="input-group-text">Objectif :</span>
                <textarea class="form-control" aria-label="Objectif"  id="objectif"></textarea>
            </div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            <button type="button" id="submit" class="btn btn-primary">Save changes</button>
        </div>
    </div>
</div>
<script>

    $(document).ready(function() {

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var events = @json($events);
       $('#calendar').fullCalendar({
            header:{
                left:'prev,next today',
                center:'title',
                right:'month, agendaWeek, agendaDay'
            },
            events: events,
            selectable:true,
            selectHelper:true,

            select: function(start, end, allDays) {
                    $('#ResModal').modal('toggle');
                    $('#submit').click(function () {
                        var title =$("#title").val();
                        var objectif =$("#objectif").val();
                        var room_id =$("#room_id").val();
                        var start = moment(start).format('YYYY-MM-DD');
                        var end = moment(end).format('YYYY-MM-DD');
                //
                //
                    });


                    $.ajax({

                        url:"{{route('MyReservations.store')}}",
                        type:"POST",
                        dataType:'json',
                        data:{title, start, end, room_id, objectif},
                        success:function(response)
                        {
                         consol.log(response)

                     },

                    })


            },

        })
    });

</script>

MyController :

public function  index(){

    $events=array();
    $reservations = reservation::all();
    foreach ($reservations as $reservation){
        $events[] = [
            'title'   =>$reservation->title,
         'start'   =>$reservation->start,
        'end'   =>$reservation->end,
        ];
    }
    $room=room::all();
    return view('/MyReservations', compact('events' ,'room'));

}

public function store(Request $request)
{
   $request->validate([
        'title' =>'required|string',
        'objectif' =>'required|string',
        'room_id' =>'required',
    ]);
    $reservation =reservation::create ([
        'objectif' => $request->objectif,
        'start' => $request->start,
        'end' => $request->end,
        'user_id' => Auth::user()->id,
        'room_id' => $request->room_id,
    ]);
    return response()->json($reservation);

}

and i keep getting this error :

Uncaught TypeError: Cannot convert undefined or null to object

at entries (<anonymous>)

at e (jquery.min.js:4:7727)

at Ab (jquery.min.js:4:7608)

at Ab (jquery.min.js:4:7631)

at Ab (jquery.min.js:4:7631)

at Function.r.param (jquery.min.js:4:7918)

at Function.ajax (jquery.min.js:4:12227)

at Et.e.constructor.select (MyReservations:121:27)

at constructor.publiclyTrigger (fullcalendar.min.js:9:16482)

at Et.e.constructor.publiclyTrigger (fullcalendar.min.js:8:30673)

when I select a date and try to add an event...and i fill in all the info ( till here everything works perfectly) but once i click the 'save changes button ' i see this error in console, and no data is added to ma DB I think the problem is in my store function in MyController or in the $.ajax

NOTE :

  • I have imported all the important scripts and links I just couldn't share them here update : the validation is not even working so the store method seems like it is not being called in the first place !! PLEASE HELP ME SOLVE IT
0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

For one your object is incorrectly formed, it has to be key: value and you use just value the other thing is that your AJAX is added out of the submit event, and all of those var title.. don't exists out of the context, so just do this:

$('#submit').click(function () {
     var title =$("#title").val();
     var objectif =$("#objectif").val();
     var room_id =$("#room_id").val();
     var start = moment(start).format('YYYY-MM-DD');
     var end = moment(end).format('YYYY-MM-DD');
// AFTER THE VARS WITHIN THE CLICK EVENT HANDLER
	$.ajax({
	   url:"{{route('MyReservations.store')}}",
       type:"POST",
       dataType:'json',
       data:{title: title, start: start, end: end, room_id: room_id, objectif: objectif},
       success:function(response) {
   			consol.log(response);
	},

});
2 likes
kylie's avatar
Level 1

@Nakov Thank you Sir I did what you told me, the old error is done but now I'm getting this error : " Failed to load resource: the server responded with a status of 500 (Internal Server Error) " I checked StackOverflow but I didn't find a solution for this error!

Nakov's avatar

@Hadjer25 Open the network tab and preview the request that fails for the error, or go to storage/logs/laravel.log or any of the .log files there to get a better error message. It is up for you to debug, the naming that you use does not follow any of the laravel conventions so it is hard for me to tell what can be wrong. Have fun!

1 like
kylie's avatar
Level 1

@Nakov THANK YOU very much sir , i appreciate it THANK YOU infiniment .

Please or to participate in this conversation.