Search code examples
javascriptnode-redjsonata

Using context value in a Node-RED switch node


I'm using the Switch node in Node-RED to determine if I should batch upload a buffered array (which is stored in a context object).

I would like the property to be flow.batchData.length, and the condition to be>= 100.

If I select the property type to be flow., and set the property value to be batchData, I can't then access the length attribute of the array.

I tried using a Javascript expression property type, but the following value didn't work: flow.get("batchData").length.

How can I use the length of an object stored in context in a Switch node?


Exported Node here:

[{"id":"de5265d5.0f7408","type":"switch","z":"157e7444.ffccac","name":"Should upload?","property":"flow.get(\"batchData\").length","propertyType":"jsonata","rules":[{"t":"gte","v":"100","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":820,"y":620,"wires":[["410db161.d497b"]]}]

Solution

  • The JSONata expression to be used to count the amount of elements in the array stored in your flow context variable should be:

    $count($flowContext("batchData"))

    and the configuration dialog of your switch node will look like:

    enter image description here