Inexplicable Variable Becomes Undefined Inside Function

I'm facing an issue in my Node-RED flow. I have a string injection node that sends a message with a value 'a30b74fa' to a function. However, when it reaches the function, the 'tagToCheck' variable, which should contain 'a30b74fa,' becomes undefined. I've already confirmed that the message is correct before entering the function, but something seems to be altering the message along the way. How can I resolve this issue?

image

As shown by the debug logs, the message remains correct until it reaches the function.

Here is the function code.

var tagToCheck = msg.currentTag
var results = msg.database;

var tagExists = false;
for (var i = 0; i < results.length; i++) {
    if (results[i].tag_id === tagToCheck) {
        tagExists = true;
        break;
    }
}

if (tagExists) {
    msg.payload1 = "Tag cadastrada!";
} else {
    msg.payload1 = "Tag não cadastrada!";
}

return msg;

10 Sep 18:05:54 - [info] Node-RED version: v3.1.0
10 Sep 18:05:54 - [info] Node.js version: v14.21.3

Your issue is the msg.currentTag and msg.database are not in the same message

Either move the change nodes to the same wire or join the messages.

If you want more help please supply an example flow with the the database message in an inject node, so we have an example of the input.

Here is the inject node.


My goal is to read the table from my SQLite database, which is already happening because the debug output returns the array with the values. What's missing is just to complete the validation to check if the tag I injected is contained in the table or not

Not what i asked for.
How to import/export a flow
You can use the copy value icon in debug 3 to show us the db return in msg.database.

Here is an example of how to do it inline in a single message.

[{"id":"a64548953fdaac3b","type":"inject","z":"b9860b4b9de8c8da","name":"in","props":[{"p":"currentTag","v":"1234","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":570,"y":160,"wires":[["d9dec6e565ce4d56"]]},{"id":"d9dec6e565ce4d56","type":"template","z":"b9860b4b9de8c8da","name":"simulate db","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[\n{\"tag_id\":\"1234\"},\n{\"tag_id\":\"5678\"}\n]","output":"json","x":730,"y":160,"wires":[["9e6d4ad7bb97b75b"]]},{"id":"9e6d4ad7bb97b75b","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"database","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":160,"wires":[["b25f8066f59ddfcb"]]},{"id":"92e0c3d4270ce657","type":"template","z":"b9860b4b9de8c8da","name":"simulate db","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[\n{\"tag_id\":\"2345\"},\n{\"tag_id\":\"5678\"}\n]","output":"json","x":730,"y":220,"wires":[["9e6d4ad7bb97b75b"]]},{"id":"b25f8066f59ddfcb","type":"function","z":"b9860b4b9de8c8da","name":"","func":"var tagToCheck = msg.currentTag\nvar results = msg.database;\n\nvar tagExists = false;\nfor (var i = 0; i < results.length; i++) {\n    if (results[i].tag_id === tagToCheck) {\n        tagExists = true;\n        break;\n    }\n}\n\nif (tagExists) {\n    msg.payload1 = \"Tag cadastrada!\";\n} else {\n    msg.payload1 = \"Tag não cadastrada!\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":160,"wires":[["15a71ced56e8e7f8"]]},{"id":"594ee668eb3d16db","type":"inject","z":"b9860b4b9de8c8da","name":"not in","props":[{"p":"currentTag","v":"1234","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":570,"y":220,"wires":[["92e0c3d4270ce657"]]},{"id":"15a71ced56e8e7f8","type":"debug","z":"b9860b4b9de8c8da","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload1","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":260,"wires":[]}]

Here is a join example

[{"id":"a64548953fdaac3b","type":"inject","z":"b9860b4b9de8c8da","name":"in","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1234","payloadType":"str","x":570,"y":60,"wires":[["d9dec6e565ce4d56","5edfe004614a3947"]]},{"id":"d9dec6e565ce4d56","type":"template","z":"b9860b4b9de8c8da","name":"simulate db","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"[\n{\"tag_id\":\"1234\"},\n{\"tag_id\":\"5678\"}\n]","output":"json","x":610,"y":140,"wires":[["9e6d4ad7bb97b75b"]]},{"id":"5edfe004614a3947","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"tag","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":60,"wires":[["7f4f5fe9fb6c4c1a"]]},{"id":"9e6d4ad7bb97b75b","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"db","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":140,"wires":[["7f4f5fe9fb6c4c1a"]]},{"id":"7f4f5fe9fb6c4c1a","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":970,"y":100,"wires":[["b25f8066f59ddfcb"]]},{"id":"b25f8066f59ddfcb","type":"function","z":"b9860b4b9de8c8da","name":"","func":"var tagToCheck = msg.payload.tag\nvar results = msg.payload.db;\n\nvar tagExists = false;\nfor (var i = 0; i < results.length; i++) {\n    if (results[i].tag_id === tagToCheck) {\n        tagExists = true;\n        break;\n    }\n}\n\nif (tagExists) {\n    msg.payload1 = \"Tag cadastrada!\";\n} else {\n    msg.payload1 = \"Tag não cadastrada!\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":140,"wires":[["15a71ced56e8e7f8"]]},{"id":"15a71ced56e8e7f8","type":"debug","z":"b9860b4b9de8c8da","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload1","targetType":"msg","statusVal":"","statusType":"auto","x":1010,"y":240,"wires":[]}]

But would it not be better to query db select * from db_table where tag_id = "1234" and if array length is 0 then no tag was found in query result returned for select query.

Thanks for helping! I put all the nodes in the same line and started changing the variables, and it worked, just like the example you sent me.

[{"id":"3d2a1d0a5cc0f429","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"d85742b1fd6dc763","type":"inject","z":"3d2a1d0a5cc0f429","name":"","props":[{"p":"tag","v":"f50a44fa","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":140,"wires":[["81c6cff4ce169303"]]},{"id":"9271f11ae3b0ed51","type":"debug","z":"3d2a1d0a5cc0f429","name":"debug 335","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload1","targetType":"msg","statusVal":"","statusType":"auto","x":500,"y":240,"wires":[]},{"id":"2217ccc14c242d41","type":"change","z":"3d2a1d0a5cc0f429","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"database","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":140,"wires":[["bdb74a464e115eaa"]]},{"id":"bdb74a464e115eaa","type":"function","z":"3d2a1d0a5cc0f429","name":"function 4","func":"\nvar tagToCheck = msg.currentTag\nvar results = msg.database;\n\nvar tagExists = false;\nfor (var i = 0; i < results.length; i++) {\n    if (results[i].tag_id === tagToCheck) {\n        tagExists = true;\n        break;\n    }\n}\n\nif (tagExists) {\n    msg.payload1 = \"Tag cadastrada!\";\n} else {\n    msg.payload1 = \"Tag não cadastrada!\";\n}\n\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":240,"wires":[["9271f11ae3b0ed51"]]},{"id":"81c6cff4ce169303","type":"change","z":"3d2a1d0a5cc0f429","name":"","rules":[{"t":"move","p":"tag","pt":"msg","to":"currentTag","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":140,"wires":[["9c340d6d0f241b49"]]},{"id":"9c340d6d0f241b49","type":"sqlite","z":"3d2a1d0a5cc0f429","mydb":"2534f15f3f364df2","sqlquery":"fixed","sql":"SELECT * FROM tags2","name":"","x":610,"y":140,"wires":[["2217ccc14c242d41"]]},{"id":"2534f15f3f364df2","type":"sqlitedb","db":"/home/iot/rfid2.db","mode":"RWC"}]

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