Function definition in `on start` works but I'm not sure

I defined some sub functions I use in a function node in the tab on start, it works but I'm not sure wheather ii's allowed.

on start:

pad = function (number) {
  if (number < 10) {
    return '0' + number;
  return number;

// return "YYYY-MM-DDThh:mm:ss.sss"   
timestamp = function () {
  var d = new Date();
  return d.getFullYear() +
    '-' + pad(d.getMonth() + 1) +
    '-' + pad(d.getDate()) +
    'T' + pad(d.getHours()) +
    ':' + pad(d.getMinutes()) +
    ':' + pad(d.getSeconds()) +
    '.' + (d.getMilliseconds() / 1000).toFixed(3).slice(2, 5);

on message:

msg.payload = timestamp(msg.payload);

return msg;

Not quite sure what you mean there. If it works, it is fine. :slight_smile:

Well, I had to replace more than one node in the past because new versions didn't support the features I used.
So the question is: will this work in the future? :wink:

Out of curiosity, which nodes? I can't remember any of the node red core nodes ever doing this.

Nothing is forever, but I'm fairly certain this feature is here to stay.

@cflurin variables defined in the on start should not be directly accessible in the on message tab.

The fact you can define functions like that and have pad and timestamp available in the on message tab is a bug and will be fixed. I'm not even sure how that is possible.

The correct way to define things in on start to reuse in on message is via the context object.

So you'd do:

context.pad = function ....


context.timestamp = function ...

1 Like

Thanks @knolleary for the clarification.
I had a suspect, that this wasn't your intention.

Hmmm... I suspect it's going to be far more work to 'fix' it then it is to say 'wow, look at this new feature we added without even trying'.


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