You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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:

  1. Scope resolution: By declaring fullCalendarEvents outside the forEach loop, you avoid the issue where title and start were trapped inside the loop's callback scope. Now you can access the full list of formatted events anywhere in the then block.
  2. Format matching: Each Eventbrite event is converted into an object that exactly matches what FullCalendar expects—with title and start fields. Eventbrite's start.local is an ISO 8601 string, which FullCalendar can parse directly without extra conversion.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:14:44