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

**URL:** <https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401>\
**Category:** General\
**Created:** [19 April 2021 11:51 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401 "2021-04-19T11:51:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [19 April 2021 11:51 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/1 "2021-04-19T11:51:29Z")

</div>

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`:

```auto
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`:

```auto
msg.payload = timestamp(msg.payload);

return msg;

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 April 2021 19:32 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/2 "2021-04-19T19:32:13Z")

</div>

> [@cflurin](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [19 April 2021 19:58 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/3 "2021-04-19T19:58:11Z")

</div>

> [@TotallyInformation](#):
>
> If it works, it is fine. 🙂

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?` 😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 April 2021 20:01 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/4 "2021-04-19T20:01:38Z")

</div>

> [@cflurin](#):
>
> Well, I had to replace more than one node in the past because new versions didn't support the features I used.

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

> [@cflurin](#):
>
> So the question is: `will this work in the future`

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 April 2021 20:17 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/5 "2021-04-19T20:17:47Z")

</div>

@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 ....`

and

`context.timestamp = function ...`

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [19 April 2021 20:22 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/6 "2021-04-19T20:22:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 April 2021 20:23 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/7 "2021-04-19T20:23:05Z")

</div>

> [@knolleary](#):
>
> 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.

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'.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 June 2021 20:23 UTC](https://discourse.nodered.org/t/function-definition-in-on-start-works-but-im-not-sure/44401/8 "2021-06-18T20:23:25Z")

</div>

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