In a function node - you have your own context - so to keep it easy - I used as context variable name the msg.topic which is topic1, topic2, topic3
If you click on your function node - you can directly see the context variables, which are stored with your function node, when you open your context menu.
So in the debug window you can see how the payload increases with each inject node:
With
context.set (<name>,<value>);
you can store a value, with
context.get (<name>);
you can retrieve your stored value back.
Here is the code of a function node:
context.set(msg.topic, msg.payload);
var valueOfTopic1 = context.get('topic1')||"";
var valueOfTopic2 = context.get('topic2')||"";
var valueOfTopic3 = context.get('topic3')||"";
msg.payload = { topic1: valueOfTopic1,
topic2: valueOfTopic2,
topic3: valueOfTopic3 }
return msg;
And here is the flow to test by yourself:
Zusammenfassung
[
{
"id": "49aae2568a4a87e0",
"type": "inject",
"z": "ebe8134f6b5af26c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "topic1",
"payload": "This is a string in topic1",
"payloadType": "str",
"x": 600,
"y": 960,
"wires": [
[
"365882932ba6ac11"
]
]
},
{
"id": "2572f6e462c6dcc1",
"type": "inject",
"z": "ebe8134f6b5af26c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "topic2",
"payload": "198745",
"payloadType": "num",
"x": 650,
"y": 1020,
"wires": [
[
"365882932ba6ac11"
]
]
},
{
"id": "84716c3a07f97e9e",
"type": "inject",
"z": "ebe8134f6b5af26c",
"name": "topic3: An Object",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "topic3",
"payload": "{\"type\":\"object\",\"name\":\"MyObject\"}",
"payloadType": "json",
"x": 640,
"y": 1080,
"wires": [
[
"365882932ba6ac11"
]
]
},
{
"id": "365882932ba6ac11",
"type": "function",
"z": "ebe8134f6b5af26c",
"name": "",
"func": "context.set(msg.topic, msg.payload);\nvar valueOfTopic1 = context.get('topic1')||\"\";\nvar valueOfTopic2 = context.get('topic2')||\"\";\nvar valueOfTopic3 = context.get('topic3')||\"\";\n\nmsg.payload = { topic1: valueOfTopic1,\n topic2: valueOfTopic2,\n topic3: valueOfTopic3 }\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 870,
"y": 1020,
"wires": [
[
"ad257cf107d542c7"
]
]
},
{
"id": "ad257cf107d542c7",
"type": "debug",
"z": "ebe8134f6b5af26c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1080,
"y": 1020,
"wires": []
}
]
So in your case - you need to identify the msg object from which node it comes (best is using msg.topic, if there is none - set one with a change node), then you can store the payload with a specific name in the context of your function node - and then you can retrieve the value whenever you need it in your code.
So in my example you can compare the values of the different nodes:
if (valueOfTopic1 === 'hello' && valueOfTopic2 === 7) {
// ... do something
msg.payload = "Finally a match!";
}