# Inexplicable Variable Becomes Undefined Inside Function

**URL:** <https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179>\
**Category:** General\
**Tags:** database, sqlite\
**Created:** [10 September 2023 21:08 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179 "2023-09-10T21:08:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adalbiot](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adalbiot](https://discourse.nodered.org/u/adalbiot)\
**Post date:** [10 September 2023 21:08 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/1 "2023-09-10T21:08:53Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27dc1e871d54a07d844ac5c15be8baa5b8e5293c.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7e598f29f1e7ae2a0bd535eb07eee9b5b68df73.png)

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

Here is the function code.

```auto
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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 September 2023 21:26 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/2 "2023-09-10T21:26:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adalbiot](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adalbiot](https://discourse.nodered.org/u/adalbiot)\
**Post date:** [10 September 2023 21:41 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/3 "2023-09-10T21:41:27Z")

</div>

Here is the inject node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2c1f454643aa2cb68f45756dde10cf9748a8c88.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cffb1382eb9eaa8208a7920adeed7e8a68abe386.png)

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 September 2023 21:54 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/4 "2023-09-10T21:54:44Z")

</div>

Not what i asked for.  
[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)  
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.

```auto
[{"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

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![adalbiot](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adalbiot](https://discourse.nodered.org/u/adalbiot)\
**Post date:** [10 September 2023 22:41 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/5 "2023-09-10T22:41:14Z")

</div>

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.

```auto
[{"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"}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 September 2023 22:41 UTC](https://discourse.nodered.org/t/inexplicable-variable-becomes-undefined-inside-function/81179/6 "2023-09-24T22:41:57Z")

</div>

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