Friday, 25 October 2019

RNCalendarEvents.saveEvent() issues Error: No calendar has been set

My react-native-calendar-events code is throwing an error, the error is saying No calendar has been set, but I don't know why this is happening. The same code on the legacy version of my app works on the same devices.

export async function createCalendarEvent(event) {
  const status = await RNCalendarEvents.authorizationStatus();
  console.log(status);
  if (status === "authorized" || status === "undetermined") {
    addToCalendar(event);
  } else {
    RNCalendarEvents.authorizeEventStore()
      .then(auth => {
        // handle status
        if (auth === "authorized" || auth === "undetermined") {
          addToCalendar(event);
        }
      })
      .catch(() => {
        alert("This app needs calendar access");
      });
  }
}

async function addToCalendar(event) {
  try {
    const startDate =
      Platform.OS === "ios"
        ? format(parse(event.StartDateLocal))
        : parse(event.StartDateLocal);
    const endDate =
      Platform.OS === "ios"
        ? format(parse(event.EndDateLocal))
        : parse(event.EndDateLocal);
    const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);

    const calendarEvent = allEvents.find(e => e.title === event.Title);
    if (calendarEvent) {
      alert("You have already added this event to your calendar.");
    } else {
      const title = event.Title;

      const {
        Location: {
          AddressLine1: address,
          City: city,
          StateAbbreviation: state,
          PostalCode: zip
        }
      } = event;

      const location = `${address}, ${city}, ${state}, ${zip}`;

      const settings = {
        location,
        startDate,
        endDate
      };
      RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });
    }
  } catch (e) {
    alert("Oops! Something has gone wrong with this request.");
  }
}

I recently added the Or in status === "authorized" || status === "undetermined", that somehow has kept the iOS side from crashing completely when it errors out.

I thought it might be a version issue, but I went back to the version we had and I still have this issue. So the Promise is erroring out, but why?

I put together a minimally viable product like so:

import React from "react";
import { Text, StyleSheet, View, TouchableOpacity } from "react-native";
import RNCalendarEvents from "react-native-calendar-events";
import parse from "date-fns/parse";
import format from "date-fns/format";

const ComponentsScreen = () => {
  async function addToCalendar(event) {
    console.log(RNCalendarEvents);
    try {
      RNCalendarEvents.saveEvent("Title of event", {
        startDate: "2016-08-19T19:26:00.000Z",
        endDate: "2017-08-19T19:26:00.000Z"
      })
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });
    } catch (e) {
      alert(e.message);
    }

And the RNCalendarEvents method of saveEvent() returns undefined even though the RNCalendarEvents I console log, returns all the methods available to this API. I thought this would suggest a linking issue, but it should be autolinking and at any rate I tried react-native link and still getting undefined on its methods.

I just noticed through my debugging process that react-native-calendar-events is still using index.ios.js. Could that be why I am getting back the methods for the RNCalendarEvents API, but when I try to implement them I get back undefined.



from RNCalendarEvents.saveEvent() issues Error: No calendar has been set

No comments:

Post a Comment