Unable To Create An Event In Calendar Expo React Native

1.1k Views Asked by At

I'm facing an error when I'm trying to create an event in my calendar. Please let me know the solution. Thanks

Error

[Error: createEventAsync must be called with an id (string) of the target calendar]

Component I just called below function onPress of a Button

async addCalendarEvent(date) {
        const startDate = new Date(date);
        const setting = new Date(date);
        const mergeDate = setting.setHours(setting.getHours() + 2);
        const endDate = new Date(mergeDate);
        console.log('starting date ==> ', startDate);
        console.log('end date ==> ', endDate);
        const calendarPermission = await Permissions.askAsync(Permissions.CALENDAR);
        if (calendarPermission.status === 'granted') {
            const eventId = await Calendar.createEventAsync(Calendar.DEFAULT, {
                title: 'Con Fusion Table Reservation',
                startDate: startDate,
                endDate: endDate,
                timeZone: 'Asia/Karachi'
            }).then((res) => {
                console.log('res ==> ', res)
            }).catch(err => console.log('error ==> ', err))
            console.log('event ==> ', eventId);
            // const eventId = await Calendar.createEventAsync(Calendar.DEFAULT, {


            //   });
        } else {
            console.log('permission not granted!')
        }
    }
2

There are 2 best solutions below

0
Nouveau On

Calendar: the DEFAULT constant has been removed. This decision was made because Android API doesn't provide something like default calendar. So if you want to create an event, you need to provide a valid calendar's id. Here is the example code for Creating an event in Calendar:

import * as Permissions from 'expo-permissions';
import * as Calendar from 'expo-calendar';

Now asking the user for Permissions and Creating Event:

async obtainCalendarPermission(){
      const { status } = await Permissions.askAsync(Permissions.CALENDAR);
    if (status === 'granted') {
        console.log('Permission Granted')  
         }
}

async addReservationToCalendar(date) {
    await this.obtainCalendarPermission();
    
    const caleve = await Calendar.createEventAsync("1",
        {title:'Con Fusion Table Reservation',
        startDate: new Date(Date.parse(date)),
        endDate: new Date(Date.parse(date) + 2 * 60 * 60 * 1000),
        timeZone: 'Asia/Hong_Kong',
        location: '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong'})
    Calendar.openEventInCalendar(caleve)
}

Now this will pop up an event in the device's Calendar.

P.S.Now you can complete your assignment.

0
Unico On
addNewEvent = async () => {
    try {
      //////////////////////////////////// permission
       const { status } = await Calendar.requestCalendarPermissionsAsync();
      if (status === "granted") {
         const calendars = await Calendar.getCalendarsAsync(
         Calendar.EntityTypes.EVENT
         );
         console.log("Here are all your calendars:");
         console.log({ calendars });
      }
      const defaultCalendarSource =
        Platform.OS === "ios"
          ? await getDefaultCalendarSource()
          : { isLocalAccount: true, name: "My Calendar" };

      const newCalendarID = await Calendar.createCalendarAsync({
        title: "My Calendar",
        color: "yellow",
        entityType: Calendar.EntityTypes.EVENT,
        sourceId: defaultCalendarSource.id,
        source: defaultCalendarSource,
        name: "internalCalendarName",
        ownerAccount: "personal",
        accessLevel: Calendar.CalendarAccessLevel.OWNER,
      });

      console.log(`Your new calendar ID is: ${newCalendarID}`);
      const cals = await Calendar.getCalendarsAsync();
      console.log(cals);