Get Events from Google Calendar with "minTime" and "maxTime"

Hi at all,

I ran into an Issue with getting Events via the node-red-contrib-google Node.
It´s all set up well so far and works, but with one twist, here's the setup:

I have a global object called systime were i store some date data to reuse it for different applications.
it looks like this:
systime

As you can see there are two variables set "nowIso" and "inaweek" which are perfectly formatted as ISO date string as required by the Google API.

Now i trigger a function node with an Inject to perform an API request. like this:

systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;

msg.time =  {now: now, inaweek:inaweek};
msg.payload = {calendarId:"mycalendarID@googlemail.com", maxResults: "20"};
msg.payload.timeMin = now;
msg.payload.timeMax = inaweek;
return msg;

I use the msg.time for debugging purpose and see, that in msg.time i have the same needed ISO date strings.

but I do get following error from the Google node:

"Error: Bad Request"

.....
When passing the ISO date directly like:

systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;

msg.time =  {now: now, inaweek:inaweek};
msg.payload = {calendarId:"mycalendarID@googlemail.com", maxResults: "20"};
msg.payload.timeMin = "2020-04-13T16:31:21.649Z";
msg.payload.timeMax = "2020-04-20T16:31:21.649Z";
return msg;

it works totally fine and I totally can't figure out why!
please help an obvious blockhead...

Are you sure that the time values stored in context are strings and not Date objects (which the debug panel helpfully converts to visible ISO strings for you).

To check my hypothesis try

msg.payload.timeMin = now.toISOString();
msg.payload.timeMax = inaweek.toISOString();
1 Like

thank you for your fast reponse!
I absolutely am!
the string in the screenshot above is a sting, other wise it would appear in blue.
and typeof returns string as well.

Okay with your guess I however solved it, even if I tried it quite like that before.
I had to set inaweek to a timestamp.
when doing this in my function:

systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;
inaweek = new Date(inaweek);



msg.time =  {now: now.toISOString() , inaweek:inaweek};
msg.payload = {calendarId:"myCalendarID", maxResults: "20"};
msg.payload.timeMin = now.toISOString();
msg.payload.timeMax = inaweek.toISOString();
return msg;

it works! :man_facepalming:

pretty weird, it works with the ISO String of now, but not with the String from inaweek, that i formatted the exact same way?! is this, because it is a date in the future? are there any restrictions by JS, Node.js, NR?
e.g.

//today is 13.04.2020
// this works
now = "2020-04-13T21:55:20.119Z";
now = now.toISOString();                        //even if this obviously is an ISO

//this doesnt work
inaweek = "2020-04-20T21:55:20.119Z";
inaweek = inaweek.toISOString();
// this returns
// "Error: inaweek.toISOString is not a function"

anyway, i will have learned to generate a new dateobject everytime i work with future dates!
and thank you very much Colin, you pushed me over the edge :grinning:

If systime.nowISO is a string, as I think you are saying then the above would fail because toISOString() is not a function you can call on a string.

1 Like

thank you again, colin. there seemed to be a mistake of mine that i overlooked.
the solution is the following:

storing the ISO strings as before in my systime function,
comments for potential future Datefunction victims.

time = new Date();                                          // new date object

hour = time.getHours();                                     // extract the stuff
minute = time.getMinutes();
day = time.getDay();
date = time.getDate();
month = time.getMonth()+1;                                  // necessary because months are indexed 0-11
year = time.getFullYear();

nowIso = time.toISOString();                                // convert now to ISO string
inaweekstamp = Date.now()+604800000;                        // for inaweek timestamp + 7days in millisecs 
inaweek = new Date(inaweekstamp).toISOString();             // new dateobject from that and convert to ISO

var showhour;
var showminute;

if(minute < 10){
    showminute = "0"+minute;
}else{showminute = minute}
if(hour < 10){
    showhour = "0"+hour;
}else{showhour = hour}

global.set("systime",{"hour":hour,"minute":minute, "day":day, "date":date, "month": month, "year":year,"showhour":showhour, "showminute":showminute, "nowIso":nowIso,inaweek:inaweek});
node.status({text:"System Time: "+showhour+":"+showminute})

where now is safe, that nowISO and inaweek are ISO strings without a doubt

recall the variables anywhere else

systime = global.get("systime");
now = systime.nowIso;
inaweek = systime.inaweek;


msg.time =  {now: now, inaweek:inaweek};
msg.payload = {calendarId:"stebe41@googlemail.com", maxResults: "20"};
msg.payload.timeMin = now;
msg.payload.timeMax = inaweek;
return msg;

Personally I would do it the other way round, keep everything as Date objects (so you can do things like add a week or convert to local time or whatever) and only convert to a string when you need to, which won't be very often.

You are right in most cases. In fact I want to look up multiple Google cals which requests are triggered separately, so in my particular case, I want do do the toISOString() once per period centralized!
Thanks a lot!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.