# How to set a flow.context to a default value if the variable turns out to be undefined

**URL:** <https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234>\
**Category:** General\
**Tags:** function-node\
**Created:** [24 May 2024 12:44 UTC](https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234 "2024-05-24T12:44:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [24 May 2024 12:44 UTC](https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234/1 "2024-05-24T12:44:57Z")

</div>

> [@Noob set flow variable question](https://discourse.nodered.org/t/noob-set-flow-variable-question/40245/4):
>
> You cannot do a 'set if not defined' type of rule in the Change node,

In a function node,

how to set a flow.context to a default value, (say 0), if the variable it is expecting turns out to be _undefined_ ?

I have this in a function node, however sometimes, the msg.payload value turns out to be undefined, (or null? I am not sure), so the function node doesn't run and gives error.

"TypeError: Cannot read properties of undefined (reading 'date')"

```auto
flow.set(["mr_1", "mr_2", "mr_3", "mr_4", "mr_5", "mr_6", "mr_7", "mr_8", "mr_9", "mr_10"],
[msg.payload[0][0].date,
 msg.payload[0][1].date,
 msg.payload[0][2].date,
 msg.payload[0][3].date,
 msg.payload[0][4].date,
 msg.payload[0][5].date,
 msg.payload[0][6].date,
 msg.payload[0][7].date,
 msg.payload[0][8].date,
 msg.payload[0][9].date]);

```

---

<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:** [24 May 2024 13:03 UTC](https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234/2 "2024-05-24T13:03:52Z")

</div>

Your code will only ever work if the inner array objects ALL have `date` properties. Also, you are restricting yourself to only 10 outputs - not good if you ever need to change that.

A better approach would be something like this:

```javascript
const data = Array.isArray(msg.payload) ? msg.payload[0] : []
const out = []

data.forEach( i => {
    if (i.date) out.push( i.date )
    else out.push( null )
}

flow.set( 'mr', out )

```

Now you only have 1 flow variable to manage and it contains the correct number of source entries.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [24 May 2024 13:41 UTC](https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234/3 "2024-05-24T13:41:01Z")

</div>

I asked you bread , you gave me cake!!

![Arrodillarse Dani Martinez GIF - Arrodillarse Dani Martinez Got Talent Espana GIFs](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27fc4282ca5402c15daf7271523d3a05d16d1e4b.gif)

---

<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:** [7 June 2024 13:42 UTC](https://discourse.nodered.org/t/how-to-set-a-flow-context-to-a-default-value-if-the-variable-turns-out-to-be-undefined/88234/4 "2024-06-07T13:42:00Z")

</div>

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