# Function with two Inputs to one Output

**URL:** <https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368>\
**Category:** General\
**Tags:** function-node\
**Created:** [5 January 2025 12:57 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368 "2025-01-05T12:57:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tech152](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@tech152](https://discourse.nodered.org/u/tech152)\
**Post date:** [5 January 2025 12:57 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368/1 "2025-01-05T12:57:56Z")

</div>

Hi,  
i have a flow and need help with a function.  
I have two msg.payload as Input.  
In this function should be converted to msg.payload.lat and the other to msg.payload.lon

Can me help someone?

Thanks

```auto
[
    {
        "id": "c6f220e14ed5db21",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "1ef84508e36156e5",
        "type": "string",
        "z": "c6f220e14ed5db21",
        "name": "",
        "methods": [
            {
                "name": "left",
                "params": [
                    {
                        "type": "num",
                        "value": "10"
                    }
                ]
            }
        ],
        "prop": "payload",
        "propout": "payload",
        "object": "msg",
        "objectout": "msg",
        "x": 990,
        "y": 320,
        "wires": [
            [
                "42514e6f1ba652d3"
            ]
        ]
    },
    {
        "id": "c57fd874b886502a",
        "type": "inject",
        "z": "c6f220e14ed5db21",
        "name": "Test",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "10",
        "topic": "",
        "payload": "7.12345000 50.43209000",
        "payloadType": "str",
        "x": 830,
        "y": 360,
        "wires": [
            [
                "1ef84508e36156e5",
                "8d8456b9327f450f"
            ]
        ]
    },
    {
        "id": "8d8456b9327f450f",
        "type": "string",
        "z": "c6f220e14ed5db21",
        "name": "",
        "methods": [
            {
                "name": "right",
                "params": [
                    {
                        "type": "num",
                        "value": "11"
                    }
                ]
            }
        ],
        "prop": "payload",
        "propout": "payload",
        "object": "msg",
        "objectout": "msg",
        "x": 990,
        "y": 400,
        "wires": [
            [
                "24340e2ea9a25ba0"
            ]
        ]
    },
    {
        "id": "42514e6f1ba652d3",
        "type": "convert",
        "z": "c6f220e14ed5db21",
        "name": "",
        "convertTo": "number",
        "x": 1230,
        "y": 320,
        "wires": [
            [
                "d50579ee36a1b74e",
                "69ce2d6eeada3c4c"
            ]
        ]
    },
    {
        "id": "24340e2ea9a25ba0",
        "type": "convert",
        "z": "c6f220e14ed5db21",
        "name": "",
        "convertTo": "number",
        "x": 1230,
        "y": 400,
        "wires": [
            [
                "9216946889be3c79",
                "69ce2d6eeada3c4c"
            ]
        ]
    },
    {
        "id": "d50579ee36a1b74e",
        "type": "debug",
        "z": "c6f220e14ed5db21",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1400,
        "y": 260,
        "wires": []
    },
    {
        "id": "9216946889be3c79",
        "type": "debug",
        "z": "c6f220e14ed5db21",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1400,
        "y": 460,
        "wires": []
    },
    {
        "id": "747b0e5b64b8a97e",
        "type": "debug",
        "z": "c6f220e14ed5db21",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.lat",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1680,
        "y": 320,
        "wires": []
    },
    {
        "id": "69ce2d6eeada3c4c",
        "type": "function",
        "z": "c6f220e14ed5db21",
        "name": "function 2",
        "func": "\nvar lat;\nvar lon;\n\n//msg.payload = { \"name\": \"Worldmap\", \"lat\": lat, \"lon\": lon }\n\n\nmsg.payload = { \"name\":\"Worldmap\", \"lat\":51.05, \"lon\":-1.35 }\n\nreturn msg;\n\n\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1480,
        "y": 360,
        "wires": [
            [
                "747b0e5b64b8a97e",
                "d47a069cb4b5aa48"
            ]
        ]
    },
    {
        "id": "d47a069cb4b5aa48",
        "type": "debug",
        "z": "c6f220e14ed5db21",
        "name": "debug 7",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload.lon",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1680,
        "y": 400,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 January 2025 13:35 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368/2 "2025-01-05T13:35:43Z")

</div>

Messages are processed by the `function` node one at a time. So you need to add a `join` node before the `function` node to combine the two messages into one msg being sent to the `function` node.

Is the data coming from two inputs? If not, why not start by putting the lat and lon in one msg?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [5 January 2025 13:43 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368/3 "2025-01-05T13:43:46Z")

</div>

This question crops-up quite often.  
There is an article in the Cookbook about the 'join' node (that @zenofmud mentioned using).  
[https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

---

<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:** [5 January 2025 13:53 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368/4 "2025-01-05T13:53:22Z")

</div>

Looking at your flow, it appears that the lat and lon are originating in one message. If that is the case then don't split it into two messages, keep it in one.

Something like this should do it, fed directly from the inject node

```auto
// split the input string into two array elements
const split = msg.payload.split(' ')
msg.payload = { "name":"Worldmap", "lat":Number(split[1]), "lon":Number(split[0])}
return msg;

```

---

<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:** [5 April 2025 13:53 UTC](https://discourse.nodered.org/t/function-with-two-inputs-to-one-output/94368/5 "2025-04-05T13:53:56Z")

</div>

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