Hello,
I'm trying to get a list of events from the Calendar api. Specifically the calendar.events.list api.
On the https://developers.google.com/calendar/v3/reference/events/list it states that it should return events from the calendar. When using the base code from the quickstart https://developers.google.com/calendar/quickstart/nodejs it works perfectly. But i want the listEvents function to return an object and not just console out the events. So when i change the listEvents function from
``function listEvents(auth) {
const calendar = google.calendar({version: 'v3', auth});
calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
console.log('Upcoming 10 events:');
events.map((event, i) => {
const start = event.start.dateTime || event.start.date;
console.log(${start} - ${event.summary}`);
});
} else {
console.log('No upcoming events found.');
}
});
}
to
```function listEvents(auth) {
const calendar = google.calendar({version: 'v3', auth});
const results = calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
});
console.log(results);
}
results is just undefined. Am i missing something?
Thanks in advanced.
Greetings! If you don't pass a callback to the calendar.events.list function, it will assume you want a promise :) This method is async. Try something like this:
const calendar = google.calendar({version: 'v3', auth});
const res = await calendar.events.list({
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
});
console.log(res.data);
}
Let us know how that goes!
Hi JustinBackwith,
Thanks for the reply. I've tried handling it as an async method and it acts the same. I've used the await method suggested along with the .then method and both still return undefined.
Can you share how you're importing the library?
I'm using the nodejs quickstart guide except trying to change the output from a console out to returning the eventsList object.
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete credentials.json.
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const TOKEN_PATH = 'token.json';
Fixed the error. The quickstart says to use npm install googleapis@27 --save but after uninstalling it and using the most recent version it works.
The quickstart should be updated to say npm install googleapis --save instead of @27
@ericpak i am doing it in inline ediditor of dialogflow, and having the same error, can anyone help me solve this?
Most helpful comment
Fixed the error. The quickstart says to use
npm install googleapis@27 --savebut after uninstalling it and using the most recent version it works.The quickstart should be updated to say
npm install googleapis --saveinstead of @27