Hello Guys,
I have doubt, Actually i need Two different inject node and one function node to which both inject node is connected.
One inject node injects simple JSON data say
{
"name" : "xyz",
"age" : 12,
"address" : "new street"
}
now i want such type of function in function node which changes my age or may be any other value of above JSON data with another inject node connected to it.
Hi, I can offer you a solution using a function node easily enough but I suspect when you come to use this in a real world situation you wont be injecting values & the solution might not be that useful.
The issue is messages never travel at the same time so you either have to store the "person" for later modification or use a join node. For demonstration purposes I'll store the "person" in flow context.
Any how, I suspect you are more interested in the function code - essentially you can loop through the keys and assign the value from your payload to the other object.
Object.keys(msg.payload).forEach(e => {
msg.person[e] = msg.payload[e];
})
msg.payload = msg.person;
return msg;
alternatively you can use Object.assign
msg.payload = Object.assign({}, msg.person, msg.payload);
Here is a demo...
[{"id":"848f43d2.e6e2d","type":"inject","z":"dd03fa8d.2f4be8","name":"bill","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"name\" : \"bill\", \"age\" : 11, \"address\" : \"old street\" }","payloadType":"json","x":710,"y":600,"wires":[["56d4d9bc.9a1a58"]]},{"id":"dbaa0e8.4ed62f","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"age\" : 13 }","payloadType":"json","x":730,"y":700,"wires":[["3fcaa36.a4a9d5c"]]},{"id":"d8bbac91.bc274","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"height\" : 1.24 }","payloadType":"json","x":740,"y":820,"wires":[["3fcaa36.a4a9d5c"]]},{"id":"56d4d9bc.9a1a58","type":"change","z":"dd03fa8d.2f4be8","name":"","rules":[{"t":"set","p":"person","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":620,"wires":[["c8e5b450.5f6978"]]},{"id":"5001aa48.838754","type":"function","z":"dd03fa8d.2f4be8","name":"v1","func":"if(!msg.person) return null;\n\nObject.keys(msg.payload).forEach(e => {\n msg.person[e] = msg.payload[e];\n})\nmsg.payload = msg.person;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1150,"y":760,"wires":[["947a602b.b0eb2","54a09ccc.078a64"]]},{"id":"3fcaa36.a4a9d5c","type":"change","z":"dd03fa8d.2f4be8","name":"","rules":[{"t":"set","p":"person","pt":"msg","to":"person","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":760,"wires":[["5001aa48.838754"]]},{"id":"53161ace.08ffa4","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"age\" : 14 }","payloadType":"json","x":730,"y":740,"wires":[["3fcaa36.a4a9d5c"]]},{"id":"9a0d21bd.0837","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"height\" : 1.23 }","payloadType":"json","x":740,"y":780,"wires":[["3fcaa36.a4a9d5c"]]},{"id":"947a602b.b0eb2","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1330,"y":800,"wires":[]},{"id":"c8e5b450.5f6978","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1330,"y":620,"wires":[]},{"id":"23fca80c.3aaef8","type":"inject","z":"dd03fa8d.2f4be8","name":"ted","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"name\" : \"ted\", \"age\" : 12, \"address\" : \"new street\" }","payloadType":"json","x":710,"y":640,"wires":[["56d4d9bc.9a1a58"]]},{"id":"72ffc8c0.bb55e8","type":"function","z":"dd03fa8d.2f4be8","name":"v2","func":"if(!msg.person) return null;\nmsg.payload = Object.assign({}, msg.person, msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1150,"y":940,"wires":[["f076a1fd.66a2","2b0c0d7.5f996f2"]]},{"id":"54a09ccc.078a64","type":"change","z":"dd03fa8d.2f4be8","name":"","rules":[{"t":"set","p":"person","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1340,"y":760,"wires":[[]]},{"id":"f076a1fd.66a2","type":"change","z":"dd03fa8d.2f4be8","name":"","rules":[{"t":"set","p":"person","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1340,"y":940,"wires":[[]]},{"id":"2b0c0d7.5f996f2","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1330,"y":980,"wires":[]},{"id":"80416174.bd45b","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"age\" : 13 }","payloadType":"json","x":730,"y":880,"wires":[["20940385.4525ec"]]},{"id":"62e7cf46.95886","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"height\" : 1.24 }","payloadType":"json","x":740,"y":1000,"wires":[["20940385.4525ec"]]},{"id":"20940385.4525ec","type":"change","z":"dd03fa8d.2f4be8","name":"","rules":[{"t":"set","p":"person","pt":"msg","to":"person","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":940,"wires":[["72ffc8c0.bb55e8"]]},{"id":"fa35f570.91fb08","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"age\" : 14 }","payloadType":"json","x":730,"y":920,"wires":[["20940385.4525ec"]]},{"id":"2a80c7e1.183828","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"height\" : 1.23 }","payloadType":"json","x":740,"y":960,"wires":[["20940385.4525ec"]]}]
-
Use context to save the first injected message as "template", then capture & identify second message, grab the age value and manipulate the value in your "template", finally send to output
-
Instead code the "template" inside your function node from start, then just send the new age value, do the manipulation of the value, send the result to output. Only one inject node needed in this case
Thank you so much @Steve-Mcl ,it works for me
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.