如何将Eventbrite获取的事件数据转换为FullCalendar所需格式
Got it, let's work through this step by step. You're already successfully fetching Eventbrite data—now we just need to transform that data into the format FullCalendar expects, and fix the scope issue so you can access the event details properly.
Here's how to adjust your code:
const token = 'mytoken'; const url = 'https://www.eventbriteapi.com/v3/users/me/events/?&expand=venue'; function getEB() { let fetchData = { headers: new Headers({ 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' }), } fetch(url, fetchData) .then(response => response.json()) .then(data => { // Create an array to store events in FullCalendar's required format const fullCalendarEvents = []; data.events.forEach(element => { // Map Eventbrite fields to FullCalendar's structure const calendarEvent = { title: element.name.text, start: element.start.local, // Optional: Add more fields if you need them // end: element.end.local, // url: element.url, // location: element.venue?.address?.localized_address_display // Safely access venue data if it exists }; fullCalendarEvents.push(calendarEvent); }); // Now you have access to all transformed events in fullCalendarEvents console.log('FullCalendar-ready events:', fullCalendarEvents); // Initialize or update your FullCalendar instance const calendarEl = document.getElementById('calendar'); // Make sure this matches your element ID const calendar = new Calendar(calendarEl, { events: fullCalendarEvents }); calendar.render(); // If your calendar is already initialized, use this instead to add events dynamically: // calendar.addEventSource(fullCalendarEvents); }).catch(error => console.error(error)); }
Key fixes and explanations:
- Scope resolution: By declaring
fullCalendarEventsoutside theforEachloop, you avoid the issue wheretitleandstartwere trapped inside the loop's callback scope. Now you can access the full list of formatted events anywhere in thethenblock. - Format matching: Each Eventbrite event is converted into an object that exactly matches what FullCalendar expects—with
titleandstartfields. Eventbrite'sstart.localis an ISO 8601 string, which FullCalendar can parse directly without extra conversion. - Optional extras: I added comments for common additional fields (like end time, event URL, or venue location) that you might want to include if needed. Use optional chaining (
?.) when accessing venue data to avoid errors if an event doesn't have a venue attached.
内容的提问来源于stack exchange,提问作者jonboy
相关产品推荐
相关产品推荐

