Question: Creating a subflow for dynamic accessing a message (one of a given set)

Hey,

I am using a subflow to count messages. Since I want to do it more generic, I currently addet an InputPattern via environment..

I do the following:

image
which is working like this:

The SubFlow detects node Object msg.payload.Hugo and marks it.

Now I would like to be more flexible and seek for more hand just one level of Object.

So something like finding this:

Input: "msg.payload.Hugo;msg.topic.Hugo"

Currently I only know how to adress the property of a known object. Is there a generic way to do this or pass a msg path like other nodes to a subnode?

Here is the flow:

[{"id":"3e971524.dac1aa","type":"subflow","name":"Subflow 2","info":"","category":"","in":[{"x":60,"y":40,"wires":[{"id":"d02b4371.12ff3"}]}],"out":[{"x":320,"y":40,"wires":[{"id":"d02b4371.12ff3","port":0}]}],"env":[{"name":"InputPattern","type":"str","value":"Hugo;Ernst;Martin"}],"meta":{},"color":"#DDAA99"},{"id":"d02b4371.12ff3","type":"function","z":"3e971524.dac1aa","name":"","func":"var a=flow.get(\"$parent.demo\")||\"\";\nflow.set(\"$parent.demo\",a);\nvar InputPattern=env.get(\"InputPattern\")||\"unbekannt\";\nvar sSplit=InputPattern.split(\";\");\nmsg.touch=\"true\";\n//just short output first 10 (if present)\nfor (iIndex=0; iIndex<sSplit.length; iIndex++)\n{\n    node.warn(iIndex + \":\"+ sSplit[iIndex]);\n    if (msg.payload.hasOwnProperty(sSplit[iIndex]))\n    {\n        node.warn(\"found item\");\n        msg.payload[sSplit[iIndex]].found=true;\n    }\n    else\n    {\n    node.warn(\"NOT found item\");\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":40,"wires":[[]]},{"id":"d7137cc.d7f9e8","type":"subflow:3e971524.dac1aa","z":"b68bfc11.f4d7d","name":"","env":[],"x":340,"y":100,"wires":[["d4cc8977.761cb8"]]},{"id":"6218fdb4.62be24","type":"inject","z":"b68bfc11.f4d7d","name":"Hugo","props":[{"p":"payload.Hugo.Value","v":"OK","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":80,"wires":[["d7137cc.d7f9e8"]]},{"id":"d4cc8977.761cb8","type":"debug","z":"b68bfc11.f4d7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"368c76c.766918a","type":"inject","z":"b68bfc11.f4d7d","name":"Tim","props":[{"p":"payload.Tim.Value","v":"OK","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":120,"wires":[["d7137cc.d7f9e8"]]}]

I think what you are looking for is bracket notation where you can access an object property by a variable name.

Also, if you want to iterate an objects properties, you will need Object.keys() or Object.entries() all of which are documented (and the pages often have a dynamic playground) on the mdn pages

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.