Why does my output deliver NaN/Cannot Read

Whenever pressing the inject button, I have it to pre count function but whenever it delivers the last function where it adds all the other msgs the output just responds with NaN. Not sure what the problem is

[{"id":"d5827792b6feb906","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"a86549d83c959ac5","type":"debug","z":"d5827792b6feb906","name":"debug 40","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"total","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":540,"wires":[]},{"id":"b13ff12fc6c33052","type":"function","z":"d5827792b6feb906","name":"function 41","func":"var count = context.get(\"count\") || 0;\ncount++\ncontext.set(\"count\", count)\nmsg.payload1 = count\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":480,"wires":[["cdd1555aed909682"]]},{"id":"cdd1555aed909682","type":"function","z":"d5827792b6feb906","name":"function 42","func":"var total = 0;\nvar v1 = msg.payload1 \nvar v2 = msg.payload2\nvar v3 = msg.payload3 \n\ntotal = v1 + v2 + v3\n\nmsg.total = total\nmsg.v1 = v1\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":540,"wires":[["a86549d83c959ac5","3bc774f3ce283059"]]},{"id":"9b441a8442b9af40","type":"inject","z":"d5827792b6feb906","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":540,"wires":[["b13ff12fc6c33052","4abba7795b3f68ee","9ddace5146e923eb"]]},{"id":"4abba7795b3f68ee","type":"function","z":"d5827792b6feb906","name":"function 43","func":"var count = context.get(\"count\") || 0;\ncount++\ncontext.set(\"count\", count)\nmsg.payload2 = count\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":540,"wires":[["cdd1555aed909682"]]},{"id":"9ddace5146e923eb","type":"function","z":"d5827792b6feb906","name":"function 44","func":"var count = context.get(\"count\") || 0;\ncount++\ncontext.set(\"count\", count)\nmsg.payload3 = count\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":600,"wires":[["cdd1555aed909682"]]},{"id":"3bc774f3ce283059","type":"debug","z":"d5827792b6feb906","name":"debug 42","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"v1","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":440,"wires":[]}]

Welcome to the forum @Twellow

Unfortunately you have not posted the flow in a way that we can use.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Messages do not arrive at function 42 at the same time. So when payload1 is there 2, 3 are missing, so your maths is
1+undefined+undefined which results in a NaN not a number.
have a look at the join node to join all the messages.

See this article in the cookbook for an example of how to join messages into one object.

I corrected your post.

you are falling into a beginners trap of imagining the messages will magically arrive at the same time from multiple wires and magically merge into a single msg object.

msg will arrive with msg.payload1 then a DIFFERENT msg will arrive with ONLY msg.payload2 and so on.

this is easy to see when you put a debug into the function 42:

node.warn({
    payload1: msg.payload1,
    payload2: msg.payload2,
    payload3: msg.payload3,
});

see how there are 3 outputs for a single inject?


you need to either do this in series or use the join node.