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:
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:
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).
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:
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
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
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!