# Join Node As Function Node

**URL:** <https://discourse.nodered.org/t/join-node-as-function-node/42489>\
**Category:** General\
**Created:** [11 March 2021 14:46 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489 "2021-03-11T14:46:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [11 March 2021 14:46 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/1 "2021-03-11T14:46:17Z")

</div>

Is it possible to create a function node that combines two payloads while still having the functionallity?

---

<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:** [11 March 2021 15:01 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/2 "2021-03-11T15:01:19Z")

</div>

Welcome to the forum @ivexy24

Can you explain in more detail what you mean and why you don't want to use the Join node?

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [11 March 2021 15:11 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/3 "2021-03-11T15:11:37Z")

</div>

Im working on some project and i would like to make a custom function that combines join node and another function in 1 function

---

<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:** [11 March 2021 15:19 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/4 "2021-03-11T15:19:08Z")

</div>

> [@ivexy24](#):
>
> combines join node and another function in 1 function

Why do you want to do that? It is certainly possible using the node context but is surprisingly tricky so I would not recommend that as a starter for a beginner. But if you really want to do it then have a look at the Storing Data section in the node red docs page Writing Functions, which presumably you have already read if you are contemplating such a task.  
Really though, stand on the shoulders of those who have come before and use the Join node.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 08:51 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/5 "2021-03-16T08:51:27Z")

</div>

I get that. My boss gave me an assingment to try and implement that. Im quite new to node red so i dont know how should i do it.I'll check the storing data again and hopefully i manage to complete the task.  
Thanks for the answer.

---

<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:** [16 March 2021 09:06 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/6 "2021-03-16T09:06:44Z")

</div>

The first thing to do is to define exactly what you want the function to do. The Join node has many options and the more of those you need to support the more complex it becomes.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 09:18 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/7 "2021-03-16T09:18:01Z")

</div>

Let me try to be as clear as possible. I want to have 1 custom node that has functionallity of a join node and simple function node. So that when i join them i can access the joined payloads in the custom node and after it write the function.If u understand what i mean great but i think u wont.Im coding for 5 months so i dont know how to properly explain what i want to do:D

---

<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 March 2021 09:28 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/8 "2021-03-16T09:28:59Z")

</div>

use `msg.topic` as a property key name and `context` to store the incoming payload values.

```auto
var key = msg.topic;
if(!key) key = " __nokey__";

var joined = context.get("joined") || {}; //get the memory object || or create new empty object {}
joined[key] = msg.payload; //store the payload in the joined object under the key
context.set("joined", joined); //store the joined object for later calls to the function

//
// now you can use joined.myKeyName or joined["some-key:with--odd--chars"] 
//

msg.payload = joined; //pass the joined object to next node
return msg;

```

^ that is untested but enough to get you going.

Read the node-red docs - especially around writing functions and context.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 10:01 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/9 "2021-03-16T10:01:07Z")

</div>

This is very helpfull.I have just 1 question.In my project i have 3 Datapoints which is an object containing some data.When I use ur method i get 3 msgs in debugger.How do i make it to return only 1 msg which is an object containing my joined nodes.

---

<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 March 2021 10:03 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/10 "2021-03-16T10:03:23Z")

</div>

use `Object.keys(joined).length`

if it == 3 then `return msg` otherwise `return null`;

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 10:42 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/11 "2021-03-16T10:42:00Z")

</div>

And how could i put count like its in join node?

---

<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 March 2021 10:49 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/12 "2021-03-16T10:49:38Z")

</div>

Several ways.

One such way...

```auto
const REQUIRED_COUNT = 3;
var key = msg.topic;
if(!key) key = " __nokey__";

var joined = context.get("joined") || {}; //get the memory object || or create new empty object {}
joined[key] = msg.payload; //store the payload in the joined object under the key
context.set("joined", joined); //store the joined object for later calls to the function

//
// now you can use joined.myKeyName or joined["some-key:with--odd--chars"] 
//

if (Object.keys(joined).length === REQUIRED_COUNT) {
    msg.payload = joined; //pass the joined object to next node
    return msg;
} 

return null; //

```

NOTE: be mindful of using `=== ` - if you send a 4th variable, it will stop working. You might elect to use `>=` instead. Up to you.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 10:59 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/13 "2021-03-16T10:59:49Z")

</div>

I have 1 more question.I made this custom function node that works as 2 combined.But now my boss wants me to make a custom node from scratch that will work like for example join node. U click what u want to do with it and it does it without writing any code.Hope u understand what i mean.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 11:00 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/14 "2021-03-16T11:00:23Z")

</div>

Is that even possible for someone whose been coding frontend dev for 5 months.

---

<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 March 2021 11:17 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/15 "2021-03-16T11:17:47Z")

</div>

Why not. gotta start somewhere. The current join node src code is available in github and there is documentation on developing nodes in the node-red docs.

However, that said, I find the task a bit odd. I understand the task of making you replicate the functionality of a join node in a function node (as it forces you to learn certain aspects of node-red) but creating a new node - not sure what value that has unless you are going to be creating nodes in your job?

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 11:33 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/16 "2021-03-16T11:33:35Z")

</div>

I am working on some scheme that will monitor the work of some pumps in some station.On scheme i have lots of nodes but there is 1 "node" that is combination of 3 nodes and whose output depends on given inputs.(IF statements).My boss want me to make a node that will do exactly that. So i must combine Join node and Function node in 1 node for it to work.Its so unclear to me idk

---

<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 March 2021 11:35 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/17 "2021-03-16T11:35:20Z")

</div>

There is a feature called "Subflow" where you can pack your nodes into a sub flow & re-use it.

To all intents and purposes, to the end user, it looks like 1 node when you use an instance of a subflow.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 11:35 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/18 "2021-03-16T11:35:40Z")

</div>

![node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76b6e87c1cb7d88b1538c429fa4828eb2efeed42.png)

---

<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:** [16 March 2021 11:39 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/19 "2021-03-16T11:39:43Z")

</div>

Have you asked him/her why he doesn't want you to use the Join node? The only reason that I can think of is if this is a training exercise. If it is for production then use the Join node. Does your boss understand the implications of what he is asking or is he also new to node-red?

If you do need to design a new node then start with [Creating Nodes : Node-RED](https://nodered.org/docs/creating-nodes/)  
Did you tell your boss you haven't actually written any code yet but have just used code provided here? We are happy to help you to learn how to do it, but we should not be writing the code for you unless you are going to donate your pay to the node-red project.

---

<div class="post-metadata">

**Author:** ![ivexy24](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@ivexy24](https://discourse.nodered.org/u/ivexy24)\
**Post date:** [16 March 2021 12:14 UTC](https://discourse.nodered.org/t/join-node-as-function-node/42489/20 "2021-03-16T12:14:32Z")

</div>

Its becouse he wants to have a custom nodes that we make in it and not the ones provided form node-red. I dont understand why not simply use the nodes that are already on, its 1000x simpler.I understand so i wont even bother u anymore. Just wanted to know how to start or something like that.

[Next page](https://discourse.nodered.org/t/join-node-as-function-node/42489.md?page=2)
