Problem with the use of the "switch" node

I have a little problem with the "swicth" node, I think I don't quite understand how to use it,


my payload["1"] is an array of strings
so for me the "swicth" node I pasted above should check if the array contains the string fill in


at the output of this "switch" node I have two "functions" nodes

(as I understand it) for me if the array contains the right string that I filled in the "swicth" node then only the "function 5" node should be executed

and given that the 2nd condition of the "swicth" node is otherwise, then if the 1st test is not validated then it will not be the "function 5" node that will be executed but it will be the "function 6" node that will be executed however even if the array doesn't contain the right string that I filled in the "switch" node, it will still execute the 2 nodes (function 5 and function 6)

Can someone explain to me what I don't understand or what I'm doing wrong

Hi @Acii
Welcome to this forum!
I have an impression of what's happening.
Yet we could replace guessing by supporting you when you paste your flow here. Keep in mind to focus on the part that's not working as you expect - that is in this case the array with some strings (perhaps put in an inject node), the switch & the 2 function nodes.

The Contains option expects a single string, not an array. It checks to see if the string contains the text in the switch.

If both functions are being executed then something very odd is going on. Disconnect the functions and replace then with debug nodes and check what you see.

Hi @ralphwetzel, thanks for your reply, I get the table by making an http request, using the GET method, I get this table from a web page accessible only from my local network so copying the flow will not help because it will not work in your network

Hi @Colin, I had not understood that, but I find it surprising that the test performed with the "switch" node is still considered valid, because if it was not considered valid, the "function 5" and "function 6" nodes should not be executed, because if I replace the string with another one that is not in the table, then the "function 5" node does not execute, but the "function 6" node executes


That is my array

image

and if I modify my "swicth" node to look for something other than what is actually in the array then I get this result (which is the result of running the "function 6" node)
image

You data looks like it is an object with a property named "1" that is an array of strings (it is hard to tell as you have cut off the screenshot)

That means the first string is in msg.payload["1"][0]

Also, looking at your debug output, several msgs are traveling in quick succession - which leads me to believe there is something else going on in your flow (hard to tell from your partial screenshot)

  1. please use the "Copy Value" button on the debug message to grab valid JSON & paste it into a reply.
  2. please copy the relevant nodes and export them for us to see what is really happening (CTRL+E to export)

Hi @Steve-Mcl that's exactly it unfortunately I can't paste it all here because it contains information I'm not allowed to share


here's the real thing i got with the info i can't share i hope it's better for you, in this object I am only interested in the table "1" but I did not find how to export only this table without the other info

and i can't paste the nodes here either because to get this object i make an http request with the GET method using an API from a web page that is only accessible from my local network so if i paste the nodes here it won't work because you are not in the same network as me.

image
I have replaced the 2 function nodes with debugging but I don't understand better why the 2 are running

Pasting the code makes life much easier for those who are trying to help, as they can drop it in an inject node and test their solution.

Only thing that you need to do is replace the sensitive data, such as passwords, ap keys, etc with REDACTED.

Modify any sensitive info.

Use the "copy value" button on the debug & "simulate" the HTTP request using inject/change/function node.


Cant really help with partial info and screenshots.

Yes I suspect but unfortunately I'm not the one creating the table so I don't choose what data is inside, and as I said the table creates itself by making an http request from a web page that is only accessible from my local network so if I copy and paste my flow here it won't work for those trying to use it as they will be making the same request as I am on a web page they can't reach from their network

I'll try that but you'll only get the array with the strings and not the complete object with all the data

but I will do

[
    {
        "id": "5e3a2204c101cdfb",
        "type": "function",
        "z": "e081cb9065ce1ce8",
        "name": "function 9",
        "func": "var test = [\"DESKTOP-GLN2UDS\", \"DESKTOP-JLK0HLN\", \"nopc\"]\nnode.warn(test);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 440,
        "wires": [
            [
                "ce333f207bd87a1d"
            ]
        ]
    },
    {
        "id": "e3a3c46622e52b98",
        "type": "inject",
        "z": "e081cb9065ce1ce8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 480,
        "y": 440,
        "wires": [
            [
                "5e3a2204c101cdfb"
            ]
        ]
    },
    {
        "id": "ce333f207bd87a1d",
        "type": "switch",
        "z": "e081cb9065ce1ce8",
        "name": "",
        "property": "payload[\"1\"]",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "\"nopc\"",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 2,
        "x": 910,
        "y": 440,
        "wires": [
            [
                "b80596abd0a96dc8"
            ],
            [
                "c8f359978047466d"
            ]
        ]
    },
    {
        "id": "c8f359978047466d",
        "type": "function",
        "z": "e081cb9065ce1ce8",
        "name": "function 10",
        "func": "let test2=0;\nnode.warn(test2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1160,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "b80596abd0a96dc8",
        "type": "function",
        "z": "e081cb9065ce1ce8",
        "name": "function 11",
        "func": "let test=1;\nnode.warn(test);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1160,
        "y": 420,
        "wires": [
            [ ]
        ]
    }
]

ADMIN NOTE: Surrounded flow with triple backticks to make flow useable

So, if you are trying to determine if an array contains the text entry, then you should NOT put quotes around the string.
image

Here is a working example...

chrome_xs81jizThh

Demo Flow (use CTRL-I to import)

[{"id":"5e3a2204c101cdfb","type":"function","z":"73b44920080e0db3","name":"function 9","func":"\nmsg.payload = {\n    \"1\": [\"DESKTOP-GLN2UDS\", \"DESKTOP-JLK0HLN\", \"nopc\"]\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1780,"y":120,"wires":[["ce333f207bd87a1d"]]},{"id":"e3a3c46622e52b98","type":"inject","z":"73b44920080e0db3","name":"","props":[{"p":"find","v":"nopc","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1630,"y":120,"wires":[["5e3a2204c101cdfb"]]},{"id":"ce333f207bd87a1d","type":"switch","z":"73b44920080e0db3","name":"","property":"payload[\"1\"]","propertyType":"msg","rules":[{"t":"cont","v":"find","vt":"msg"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1930,"y":120,"wires":[["212d42261c01c55e"],["e37ef8462c66db35"]]},{"id":"212d42261c01c55e","type":"debug","z":"73b44920080e0db3","name":"Found","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2070,"y":100,"wires":[]},{"id":"e37ef8462c66db35","type":"debug","z":"73b44920080e0db3","name":"NOT Found","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2090,"y":140,"wires":[]}]

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