Change node: set an array of objects

I am sure i am not seeing it (my bad :smiling_face_with_tear:), 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.

[
    {
        "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 :pray:

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

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

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


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

1 Like

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

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