Mats Groeneweg
Mats Groeneweg

Reputation: 41

google calenderID nodejs doesn't work with specified CalendarID

I would like to use a google calendar, if I choose the calendar ID not as primary but with the specified one, I get an error message in line 46 (const eventsArray = res.data.calendars.calID.busy). The Error message looks like this (node:49993) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calendar' of undefined. So basicly, i believe, it can't read the calender ID, can enyone help me to find my fault?

const { google } = require('googleapis');
    const { OAuth2 } = google.auth;
    const oAuth2Client = new OAuth2(
        'id_works_with_primary_calender', 
        'key_works_with_primary_calender'
    )
    
    oAuth2Client.setCredentials({
        refresh_token: 
            'works_with_primary',
    })
    
    const calendar = google.calendar( { version: 'v3', auth: oAuth2Client } );
    
    const eventStartTime = new Date()
    eventStartTime.setDate(eventStartTime.getDay() + 2)
    
    const eventEndTime = new Date()
    eventEndTime.setDate(eventEndTime.getDay() + 2)
    eventEndTime.setMinutes(eventEndTime.getMinutes() + 45)
    
    const event = {
        summary: 'Meet with David',
        location: '295 California St, San Francisco, CA 94111',
        description: 'Meeting with David to talk about the new client Project and drink some stuff',
        colorId: 1,
        start: {
            dateTime: eventStartTime,
            timeZone: 'Europe/Zurich',
        },
        end: {
            dateTime: eventEndTime,
            timeZone: 'Europe/Zurich',
        },
    }
    
    calendar.freebusy.query( {
        resource: {
            timeMin: eventStartTime,
            timeMax: eventEndTime,
            timeZone: 'Europe/Zurich',
        items: [ { id: '[email protected]' } ],
        },
    }, (err, res) => {
        if (err) return console.error('Free Busy Query Error: ', err)
        //Here is the error    
        const eventsArray = res.data.calendars.gd3q30fhkprf0ubsk3pnqkc64k@group.calendar.google.com.busy
        if(eventsArray.length === 0)
            return calendar.events.insert( { 
                calendarId: '[email protected]',
                resource: event,
            },
            err => {
                if (err) return console.error('Calender Event Creation: ' + err)
                    return console.log('Event Created: ' + event)    
        })
        return console.log("I'm Busy")
    })

Upvotes: 2

Views: 211

Answers (1)

Iamblichus
Iamblichus

Reputation: 19339

Issue:

The problem seems to be that the id includes several dots, and the program thinks these are used to access nested properties. Because of this, it is looking for the property calendar of the id gd3q30fhkprf0ubsk3pnqkc64k@group and, of course, it's not finding it, because the correct id is [email protected].

Solution:

Therefore, assuming that this id is getting returned by res.data.calendars, the problem should be solved by changing the syntax with which you are accessing the key property, from dot notation to bracket notation.

That is to say, replace this:

const eventsArray = res.data.calendars.gd3q30fhkprf0ubsk3pnqkc64k@group.calendar.google.com.busy

With this:

const eventsArray = res.data.calendars["[email protected]"].busy

Reference:

Upvotes: 1

Related Questions