Date not working if flow context not set

Hi, I have this bit of code in a function node. In line 1, I want to set the lastTimestamp to now if the flow context store is not set, but it doesn't work.

If I do the same (line 2) without getting it from the flow context, it works. What am I doing wrong?

var lastTimestamp = new Date(flow.get("lastTimestamp")) || Date.now();
var now = Date.now();

msg.payload = lastTimestamp;
msg.now = now;

return msg;

Bildschirmfoto 2021-07-22 um 11.14.59

If flow.get("lastTimestamp") returns undefined, then you are doing:

new Date(undefined)

Which results in an invalid Date object, but its not a 'false-like' value. That means the || operator doesn't do anything.

I think the following does what you want:

var lastTimestamp = new Date(flow.get("lastTimestamp") || Date.now() );

Thanks, that did the trick.

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