Petar-KreŇ°imir Petar-KreŇ°imir - 17 days ago 8
Javascript Question

Uncaught TypeError: Cannot read property 'calendar' of undefined - Google Calendar API

I'm playing around with OAuth2 and Calendar API using Javascript. I'm using a following test function to see if the event is being added to the calendar (after authentication of the user by the API).

function addTestEvent()
{

var event = {
'summary': 'Google I/O 2015',
'location': '800 Howard St., San Francisco, CA 94103',
'description': 'A chance to hear more about Google\'s developer products.',
'start': {
'dateTime': '2015-05-28T09:00:00-07:00',
'timeZone': 'America/Los_Angeles'
},
'end': {
'dateTime': '2015-05-28T17:00:00-07:00',
'timeZone': 'America/Los_Angeles'
}
};

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

request.execute(function(event) {
alert(event.htmlLink);
alert("called?");
});
}


I'm using the example from the documentation.

In the console, I get the message saying calendar is undefined (referring to the var request = ... line)

Uncaught TypeError: Cannot read property 'calendar' of undefined


I have included the

<script src="https://apis.google.com/js/api.js"></script>


in my page. What am I doing wrong here?

Also, is there a tutorial or a good example that would help me understand the whole process, because the docs are kinda thin in my opinion?

Answer

You should provide more of your code because if it's saying calendar of undefined then I'm assuming the client for gapi.client wasn't instantiated correctly.

Likely need to make sure all the authing is done before you try to access the client as the client needs to verify the credentials.

And this documentation example should be a fairly straight copy paste and modify to get a working example: developers.google.com/google-apps/calendar/quickstart/js

Comments