# Is this a bug in the function node withe respect to flow context?

**URL:** <https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167>\
**Category:** General\
**Created:** [2 April 2023 16:37 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167 "2023-04-02T16:37:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssnowa](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@ssnowa](https://discourse.nodered.org/u/ssnowa)\
**Post date:** [2 April 2023 16:37 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167/1 "2023-04-02T16:37:51Z")

</div>

In the attached flow, upon first inject a flow context variable "chargeCurrentLimit" will be initialized.  
However, upon a subsequent inject, the test in the function node, "type of flow.chargeCurrentLimit" returns "undefined", while the debug nodes accessing the same variable via $type($flowContext(flow.chargeCurrentLimit)) receive the expected result of "number".

```auto
[
    {
        "id": "4b116fb54532a9f7",
        "type": "inject",
        "z": "02e824e32f138746",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"charge_current_limit\" : 100, \"discharge_current_limit\" : -100 }",
        "payloadType": "json",
        "x": 110,
        "y": 100,
        "wires": [
            [
                "d512f75cb2e9157b",
                "9eced449383f1d63"
            ]
        ]
    },
    {
        "id": "e3a65fb2fe7c41b9",
        "type": "inject",
        "z": "02e824e32f138746",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"charge_current_limit\" : 55.2, \"discharge_current_limit\" : -100 }",
        "payloadType": "json",
        "x": 110,
        "y": 160,
        "wires": [
            [
                "d512f75cb2e9157b",
                "9eced449383f1d63"
            ]
        ]
    },
    {
        "id": "1fd130debc1a6350",
        "type": "inject",
        "z": "02e824e32f138746",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"charge_current_limit\" : 25.8, \"discharge_current_limit\" : -100 }",
        "payloadType": "json",
        "x": 110,
        "y": 220,
        "wires": [
            [
                "d512f75cb2e9157b",
                "9eced449383f1d63"
            ]
        ]
    },
    {
        "id": "0887b0fa2eb28079",
        "type": "inject",
        "z": "02e824e32f138746",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"charge_current_limit\" : 0, \"discharge_current_limit\" : -100 }",
        "payloadType": "json",
        "x": 110,
        "y": 280,
        "wires": [
            [
                "d512f75cb2e9157b",
                "9eced449383f1d63"
            ]
        ]
    },
    {
        "id": "9eced449383f1d63",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "typeof flow.chargeCurrentLimit @inject",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "$type($flowContext(\"chargeCurrentLimit\"))",
        "targetType": "jsonata",
        "statusVal": "",
        "statusType": "auto",
        "x": 410,
        "y": 160,
        "wires": []
    },
    {
        "id": "d512f75cb2e9157b",
        "type": "function",
        "z": "02e824e32f138746",
        "name": "Check for change in CCL",
        "func": "let msg2 = null;\nnode.warn(\"CCL: \" + Math.abs(msg.payload.charge_current_limit));\nif (typeof flow.chargeCurrentLimit !== \"undefined\") {\n node.warn(\"FCCL: \" + Math.abs(flow.chargeCurrentLimit));\n if (Math.abs(flow.chargeCurrentLimit) != \n Math.abs(msg.payload.charge_current_limit))\n msg2 = RED.util.cloneMessage(msg); // fire debug showing new limit\n}\nelse {\n node.warn(\"flow.chargeCurrentLimit type: \" + typeof flow.chargeCurrentLimit);\n node.warn(\"CCL type: \" + typeof msg.payload.charge_current_limit)\n}\nreturn [msg2, msg]",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 240,
        "wires": [
            [
                "a0e61ca5442b857a"
            ],
            [
                "d87cb57e67c8f319",
                "998a81f4b3d9fd55"
            ]
        ]
    },
    {
        "id": "d87cb57e67c8f319",
        "type": "change",
        "z": "02e824e32f138746",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "chargeCurrentLimit",
                "pt": "flow",
                "to": "$abs(msg.payload.charge_current_limit)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 420,
        "y": 340,
        "wires": [
            [
                "e3170eefa99548b4",
                "418865778dea68dc"
            ]
        ]
    },
    {
        "id": "b04a8211097eea8c",
        "type": "catch",
        "z": "02e824e32f138746",
        "name": "",
        "scope": null,
        "uncaught": false,
        "x": 360,
        "y": 420,
        "wires": [
            [
                "2d9ea48f5c39b2f7"
            ]
        ]
    },
    {
        "id": "2d9ea48f5c39b2f7",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "debug 30",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 580,
        "y": 420,
        "wires": []
    },
    {
        "id": "418865778dea68dc",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "typeof flow.chargeCurrentLimit",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "$type($flowContext(\"chargeCurrentLimit\"))",
        "targetType": "jsonata",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 380,
        "wires": []
    },
    {
        "id": "e3170eefa99548b4",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "done changing",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 700,
        "y": 340,
        "wires": []
    },
    {
        "id": "998a81f4b3d9fd55",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "setting new FCCL",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.charge_current_limit",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 280,
        "wires": []
    },
    {
        "id": "a0e61ca5442b857a",
        "type": "debug",
        "z": "02e824e32f138746",
        "name": "New charge current limit",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.charge_current_limit",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 240,
        "wires": []
    }
]

```

---

<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:** [2 April 2023 16:40 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167/2 "2023-04-02T16:40:34Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 April 2023 17:04 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167/3 "2023-04-02T17:04:04Z")

</div>

> [@ssnowa](#):
>
> "type of flow.chargeCurrentLimit"

You should use `flow.get("chargeCurrentLimit")` to get the value.

---

<div class="post-metadata">

**Author:** ![ssnowa](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@ssnowa](https://discourse.nodered.org/u/ssnowa)\
**Post date:** [2 April 2023 22:27 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167/4 "2023-04-02T22:27:56Z")

</div>

thanks! I realized that on my own this evening.

---

<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:** [16 April 2023 22:28 UTC](https://discourse.nodered.org/t/is-this-a-bug-in-the-function-node-withe-respect-to-flow-context/77167/5 "2023-04-16T22:28:17Z")

</div>

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