# \[SOLVED\] Multiple inputs to one msg function (noob)

**URL:** https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228
**Category:** General
**Created:** [14 June 2019 09:38 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228 "2019-06-14T09:38:08Z")
**Posts on this page:** 1
**Showing post:** 15

<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: [14 June 2019 20:03 UTC](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228/15 "2019-06-14T20:03:21Z")

</div>

> [@kwa](#):
>
> spelt `payload` incorrectly as `msg.paydoad`

I already pointed that out to OP here...

> [@Steve-Mcl](#):
>
> le1pay = msg.payload; //payload NOT paydoad 🙂

  

* * *

> [@moebius](#):
>
> What exactly would be the advantage of your solution?

Readable, portable ~(i.e. no external nodes), full control of how & when things occur, self contained (no external switches / changes etc. Each to thier own. I often find a single function node doing several relatively small task is far cleaner than the multiple nodes to achieve the same thing (though not saying that would be the case here)

> [@moebius](#):
>
> After all, it is inflexible and a lot to write

Inflexible - yes, but to be fair, all I did was add to the original code to achieve the OPs requirement.  
He didnt ask for flexible & didnt provide a specification 🤷‍♂️

**On the flexible point...**  
However, if lets say the OP had a requirement for any number of _unknown_ `topic`s to sum up & wanted it to be truly flexible he could do this dynamically...

```auto
//Premise: Store any value received against its topic & return the sum of them all;
//if topic is empty or == "go", dont store anything, just return the sum
//NOTE: Probably need error handling and type checking but should work if only known types are sent to the node.
//NOTE2: Untested - but probably works :) 
let lookup = context.get("lookup") || {}; //get/create the lookup

if(msg.topic && msg.topic != "go") {
 lookup[msg.topic] = msg.payload;//store this value in lookup against topic
 context.set("lookup", lookup) || {}; //store updated lookup for next time
}

//scan the lookup for every topic value sent & add them up
let sumval = 0;
for (var prop in lookup) {
 let v = lookup[prop];  
 if(v) //if v is "something...
   sumval += v; //add it
}

msg.topic = "go";
msg.payload = sumval;

```

100% flexible - though I suspect this is not really what is wanted either. might prove useful to someone?

---

_[View the full topic](https://discourse.nodered.org/t/solved-multiple-inputs-to-one-msg-function-noob/12228)._
