Phew! Finally, I realized what I was doing wrong. Here is the code that works with comments where I made a mistake. I wish there was a better way to find out this problem.
function getAvail(auth, dateTimeRange, calID) { console.log('auth:'+JSON.stringify(auth)); console.log('date Time Range :'+(dateTimeRange.start).toISOString()+' --->'+(dateTimeRange.end).toISOString()); console.log('calendar id to check freebusy:'+calID); var deferred = Q.defer(); // get a new deferral calendar.freebusy.query({ auth: auth, headers: { "content-type" : "application/json" }, resource:{items: [{"id" : calID}], //needed to include resource instead of sending the params directly. timeMin: (dateTimeRange.start).toISOString(), timeMax: (dateTimeRange.end).toISOString() } }, function(err, response) { console.log('Response from the Calendar service: ' + JSON.stringify(response)); if (err) { console.log('There was an error contacting the Calendar service: ' + err); deferred.reject(); // deferred reject here return; } var events = response.calendars[calID].busy; if (events.length == 0) { console.log('No upcoming events found.'); } else { console.log('busy in here...'); } deferred.resolve(response); // deferred resolve here }); return deferred.promise; // return a promise }
There was a lot of thought and looked closely at the documentation. The Google nodejs library is not particularly well documented with examples (well, it's still alpha). But here is the function for frebusy: this.freebusy = {
query: function(params, callback) { var parameters = { options: { url: 'https://www.googleapis.com/calendar/v3/freeBusy', method: 'POST' }, params: params, requiredParams: [], pathParams: [], context: self }; return createAPIRequest(parameters, callback); } };
There was additional knowledge about creating closures inside loops that I learned from SO here: Closing JavaScript inside loops is a simple practical example . Let me know if anyone else encounters similar problems. I will be happy to share my code examples.
source share