# Change node: set an array of objects

**URL:** <https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620>\
**Category:** General\
**Created:** [16 April 2023 07:33 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620 "2023-04-16T07:33:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [16 April 2023 07:33 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/1 "2023-04-16T07:33:18Z")

</div>

I am sure i am not seeing it (my bad 🥲), but I am unsuccessfully trying to set a msg property to an array of objects to feed an email node using a change node.  
I need to set the msg.attachments property to something like: [{path:"/home/pi/SailingLog.txt"}] to properly initialize the email node and send a file as attachment.  
I have been successful using a function node to set that property, but I am sure the same could be done inside the change node.  
I have tried using this flow but it doesn't work; the flow.FileName variable contains the absolute path to the file to be attached.

```auto
[
    {
        "id": "c50dd6f42e704e6a",
        "type": "change",
        "z": "0ec062a8f4963dc2",
        "name": "prepare",
        "rules": [
            {
                "t": "set",
                "p": "attachments",
                "pt": "msg",
                "to": "[{ path: flow.get(\"FileName\")}]",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "SailingLog.txt",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "Last Sailing Log file",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 300,
        "y": 840,
        "wires": [
            [
                "e14648409ce7dcd2"
            ]
        ]
    },
    {
        "id": "ae933d51732689bb",
        "type": "inject",
        "z": "0ec062a8f4963dc2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 840,
        "wires": [
            [
                "c50dd6f42e704e6a"
            ]
        ]
    },
    {
        "id": "e14648409ce7dcd2",
        "type": "debug",
        "z": "0ec062a8f4963dc2",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 480,
        "y": 840,
        "wires": []
    }
]

```

Any clue will be greatly appreciated, advanced thanks 🙏

---

<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:** [16 April 2023 07:54 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/2 "2023-04-16T07:54:09Z")

</div>

> [@pacmanmt](#):
>
> `flow.get(\"FileName\")`

This is JavaScript (i.e. function node) code.

Assuming you are using newer version of node-red with the Monaco editor, type a $ and you should get a list of autocompletions - one of them being `$flowContext`

---

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [16 April 2023 08:05 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/4 "2023-04-16T08:05:46Z")

</div>

So something like this: `[{path:$flowContext("FileName")}]`?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 April 2023 08:06 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/5 "2023-04-16T08:06:38Z")

</div>

I do not think you need to use Javascript or JSONata  
e.g.

 ![chrome_screenshot_1681632271268](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c9613b130bba11ab41913a62f4c1d2c90b95448.jpeg)  
[edit] Just for info  
The JSONata you tried was almost there  
`[{"path": $flowContext("FileName")}]`  
path needed to be in quotes as it is a property name , and in JSON property names must be quoted.

---

<div class="post-metadata">

**Author:** ![pacmanmt](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pacmanmt](https://discourse.nodered.org/u/pacmanmt)\
**Post date:** [16 April 2023 08:15 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/6 "2023-04-16T08:15:19Z")

</div>

Thank you so much, it works! 😃  
Sorry to be so dumb, but still trying to catch-up with all this stuff since I am not a developer 😜

---

<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:** [30 April 2023 08:16 UTC](https://discourse.nodered.org/t/change-node-set-an-array-of-objects/77620/7 "2023-04-30T08:16:12Z")

</div>

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