Full Calendar interfacing with MongoDB

Hello. Iam trying to send an array of events objects from my mongodb to fullcalendar. What i want is to pass the events array as a simple argument inside the calendar function here is my code.Note that databook is my events array fetching from my express/node js backend, but when i run calendar then it throws me error Undefined eventsArray. Any suggestion is welcomed thank you

    script.
                document.addEventListener('DOMContentLoaded', function (databook) {
                    var calendarEl = document.getElementById('calendar');
                    var initdate = new Date();
                    var calendar = new FullCalendar.Calendar(calendarEl, {
                        function(){
                            var eventsArray=[];
                            bookdata.forEach(function (element){
                                eventsArray.push({
                                title:element.title,
                                start:element.start,
                                end:element.end  })
                            })
                        },
                        initialView: 'dayGridMonth',
                        timeZone:'Europe/Athens',
                        initialDate: initdate,
                        handleWindowResize:true,

                        headerToolbar: {
                            left: 'prev,next today',
                            center: 'title',
                            right: 'dayGridMonth,timeGridWeek,timeGridDay'
                        },
                        eventTimeFormat:{
                            hour: 'numeric',
                            minute: '2-digit',

                        },
                        eventDisplay:'auto',
                        views:{
                         timeGrid:{
                             formatDateTime:'DD/MM/YYYY HH:mm'
                         }
                        },
                        events:eventsArray


                    });
                    calendar.addEvent()
                    calendar.render();

                });
    ```

Hi @petridis_panagiotis,

If i read the code correctly the eventsArray is defined only in the callback function and cannot be used outside.

I recommend init it outside of the function , maybe next to initdate.

Thanks
Pavel

Thanks, i will try, in general the problem is that i can not pass json objects to client side. Is there any way to construct my calendar on backend, then pass my calendar object as argument to res.render and then render it on front end?