4

I'm trying to update an event in Google Calendar using JavaScript and just cannot seem to achieve a simple update of specific variables I specify. The code below is what I've tried so far along with other similar combinations:

            var event = {};

            // Example showing a change in the location
            event = {"location": "New Address"};

            var request = gapi.client.calendar.events.update({
                'calendarId': 'primary',
                'eventId': booking.eventCalendarId, // Event ID stored in database
                'resource': event
            });

            request.execute(function (event) {
               console.log(event);
            });

Regardless of the API Reference I've attempted to follow, I've tried getting the event itself and passing that reference to attempt to update specific variables. However, using the closest working sample code above I've noticed the console advises that I'm missing the start and end dates as minimum parameters. I obviously can add this to the 'event' object, yet this isn't effective as I only wish to update the fields I specify. The only other option is simply deleting the event and creating a new one - there must be a simpler way or there is something I have completely missed out entirely.

1 Answer 1

15

Managed to fix this problem myself, if anyone happens to experience the same issue. The correct format is to use 'PATCH' versus 'UPDATE', which allows for specific fields to be updated rather than having to set up the minimum range of fields required for 'UPDATE'.

This is the correct code I found that fixed the problem as an example, including a slight initial edit by getting the event first of all:

var event = gapi.client.calendar.events.get({"calendarId": 'primary', "eventId": booking.eventCalendarId});

// Example showing a change in the location
event.location = "New Address";

var request = gapi.client.calendar.events.patch({
    'calendarId': 'primary',
    'eventId': booking.eventCalendarId,
    'resource': event
});

request.execute(function (event) {
    console.log(event);
});
3
  • 1
    This code works, but events.get is a promise and you should use .then() to get the results before executing the patch request.
    – Noel Baron
    Commented Jul 28, 2017 at 15:03
  • 1
    Thank you. I was looking for what is the param name for passing the updated event body. Unfortunately, the documentation for Node.js API is very poor. Commented Aug 2, 2018 at 15:38
  • Not sure if this is an update or not, but I'm using NodeJS and had to add ".data" to my events. So: event.data.location = "New Address"; 'resource': event.data Otherwise, errors were being thrown. But now it works! Thanks!
    – amota
    Commented Feb 14, 2022 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.