# How to Check whether There is Existing Data before Allowing Users to Insert the Data to the Database

**URL:** <https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754>\
**Category:** Dashboard\
**Created:** [23 July 2018 02:10 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754 "2018-07-23T02:10:23Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [8 February 2019 14:28 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/21 "2019-02-08T14:28:55Z")

</div>

Sorry @zenofmud , for not sharing the flow, I've actually tried to modify the flow but I could help.

```auto
[{"id":"663256c1.ef9618","type":"ui_text_input","z":"a0a2199f.f5e638","name":"name","label":"Name","tooltip":"","group":"356fac5d.d980c4","order":2,"width":0,"height":0,"passthru":false,"mode":"text","delay":"300","topic":"name","x":570,"y":280,"wires":[["9c430827.1649e8"]]},{"id":"8e6c4c9d.3b383","type":"ui_text_input","z":"a0a2199f.f5e638","name":"apellido","label":"apellido","tooltip":"","group":"356fac5d.d980c4","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":"300","topic":"apellido","x":580,"y":340,"wires":[["9c430827.1649e8"]]},{"id":"12a585f6.e531ba","type":"ui_button","z":"a0a2199f.f5e638","name":"save-button","group":"356fac5d.d980c4","order":7,"width":0,"height":0,"passthru":true,"label":"Guardar datos","tooltip":"","color":"","bgcolor":"","icon":"assignment_turned_in","payload":"{}","payloadType":"global","topic":"save-button","x":570,"y":560,"wires":[["9c430827.1649e8","228f7175.69169e"]]},{"id":"9dbf8c03.4e568","type":"debug","z":"a0a2199f.f5e638","name":"node_switch1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1140,"y":340,"wires":[]},{"id":"9c430827.1649e8","type":"join","z":"a0a2199f.f5e638","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":340,"wires":[["e4b9d7ca.a5ec88","60ccb3fe.4bcd8c"]]},{"id":"e4b9d7ca.a5ec88","type":"switch","z":"a0a2199f.f5e638","name":"switch_select","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"save-button","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":360,"wires":[["9dbf8c03.4e568","d084b396.0651d"]]},{"id":"7aa36605.3867e8","type":"debug","z":"a0a2199f.f5e638","name":"node_bd_pac","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1380,"y":320,"wires":[]},{"id":"60ccb3fe.4bcd8c","type":"debug","z":"a0a2199f.f5e638","name":"node_join","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1020,"y":280,"wires":[]},{"id":"d084b396.0651d","type":"function","z":"a0a2199f.f5e638","name":"select-query","func":"var select = {topic: \"SELECT COUNT(*) AS count FROM pacientes WHERE nombre='\" + msg.payload.name + \"'\"};\nreturn select;","outputs":1,"noerr":0,"x":950,"y":420,"wires":[["8a6f7674.c3ec98","8dd0ca48.b80278","bfb6b546.6fabd8"]]},{"id":"8a6f7674.c3ec98","type":"debug","z":"a0a2199f.f5e638","name":"node_select","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1030,"y":500,"wires":[]},{"id":"1fa2ae8b.72e9c1","type":"ui_text_input","z":"a0a2199f.f5e638","name":"edad","label":"edad","tooltip":"","group":"356fac5d.d980c4","order":5,"width":0,"height":0,"passthru":false,"mode":"number","delay":"300","topic":"edad","x":570,"y":440,"wires":[["9c430827.1649e8"]]},{"id":"951ae802.19d948","type":"ui_text_input","z":"a0a2199f.f5e638","name":"sexo","label":"sexo","tooltip":"","group":"356fac5d.d980c4","order":6,"width":0,"height":0,"passthru":false,"mode":"text","delay":"300","topic":"sexo","x":570,"y":500,"wires":[["9c430827.1649e8"]]},{"id":"ade31e4f.08585","type":"ui_text_input","z":"a0a2199f.f5e638","name":"cedula","label":"cedula","tooltip":"","group":"356fac5d.d980c4","order":4,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"cedula","x":570,"y":400,"wires":[["9c430827.1649e8","e0ca343.98933c8"]]},{"id":"13b7e857.884ca8","type":"change","z":"a0a2199f.f5e638","name":"set","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":400,"wires":[["8b7d066a.c7e1a8"]]},{"id":"8b7d066a.c7e1a8","type":"ui_button","z":"a0a2199f.f5e638","name":"clear-button","group":"356fac5d.d980c4","order":8,"width":0,"height":0,"passthru":false,"label":"Borrar campos","tooltip":"","color":"","bgcolor":"","icon":"delete","payload":"[]","payloadType":"json","topic":"","x":370,"y":400,"wires":[["663256c1.ef9618","8e6c4c9d.3b383","ade31e4f.08585","951ae802.19d948","1fa2ae8b.72e9c1","12a585f6.e531ba"]]},{"id":"228f7175.69169e","type":"debug","z":"a0a2199f.f5e638","name":"node-button","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":560,"wires":[]},{"id":"8dd0ca48.b80278","type":"mysql","z":"a0a2199f.f5e638","mydb":"698ecd0.1670e34","name":"BD_DATOS","x":1150,"y":400,"wires":[["7aa36605.3867e8"]]},{"id":"e0ca343.98933c8","type":"debug","z":"a0a2199f.f5e638","name":"cedula","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":800,"y":500,"wires":[]},{"id":"2480d039.f669e","type":"function","z":"a0a2199f.f5e638","name":"insert-query","func":"insert = {topic: \"INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('\" + msg.payload.name + \"','\" + msg.payload.apellido + \"','\" + msg.payload.cedula + \"','\" + msg.payload.edad + \"','\" + msg.payload.sexo + \"')\"};\nactionString = \"Dato ingresado satisfactoriamente!\";\nreturn [insert,actionString];\n\n\n//insert = {topic: \"INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('\" + msg.payload.name + \"','\" + msg.payload.apellido + \"','\" + msg.payload.cedula + \"','\" + msg.payload.edad + \"','\" + msg.payload.sexo + \"')\"};\n//return insert; \n","outputs":1,"noerr":0,"x":1410,"y":480,"wires":[["d63e1165.fd7fe","628ee543.03c4ac"]]},{"id":"bfb6b546.6fabd8","type":"switch","z":"a0a2199f.f5e638","name":"switch_insert","property":"payload[0].count","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"msg"},{"t":"eq","v":"1","vt":"msg"}],"checkall":"true","repair":false,"outputs":2,"x":1190,"y":460,"wires":[["975efbe.6731308","2480d039.f669e"],["db614880.d35f48","7150fef5.16df6"]]},{"id":"db614880.d35f48","type":"function","z":"a0a2199f.f5e638","name":"failure-msg","func":"actionString = \"Ya se ingresado ese nombre!\";\nmsg.payload = actionString;\nreturn msg;","outputs":1,"noerr":0,"x":1410,"y":540,"wires":[["add1cfb6.0b88e"]]},{"id":"d63e1165.fd7fe","type":"mysql","z":"a0a2199f.f5e638","mydb":"698ecd0.1670e34","name":"BD_DATOS","x":1590,"y":480,"wires":[["a6c52cd3.8dfe1"]]},{"id":"1583bc32.a442d4","type":"function","z":"a0a2199f.f5e638","name":"select-query","func":"insert = {topic: \"INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('\" + msg.payload.name + \"','\" + msg.payload.apellido + \"','\" + msg.payload.cedula + \"','\" + msg.payload.edad + \"','\" + msg.payload.sexo + \"')\"};\nreturn insert; ","outputs":1,"noerr":0,"x":1030,"y":620,"wires":[[]]},{"id":"628ee543.03c4ac","type":"debug","z":"a0a2199f.f5e638","name":"node_insert","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1600,"y":420,"wires":[]},{"id":"add1cfb6.0b88e","type":"debug","z":"a0a2199f.f5e638","name":"node_fallo","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1650,"y":580,"wires":[]},{"id":"a6c52cd3.8dfe1","type":"debug","z":"a0a2199f.f5e638","name":"node_bdpacientes","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1800,"y":520,"wires":[]},{"id":"7150fef5.16df6","type":"debug","z":"a0a2199f.f5e638","name":"node_siwtch3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1410,"y":620,"wires":[]},{"id":"975efbe.6731308","type":"debug","z":"a0a2199f.f5e638","name":"node_siwtch2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1380,"y":400,"wires":[]},{"id":"a942a4fe.805278","type":"ui_toast","z":"a0a2199f.f5e638","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"","topic":"Ingreso correcto del paciente","name":"Ingreso correcto del paciente","x":1640,"y":340,"wires":[[]]},{"id":"87508a4f.47d1e8","type":"ui_toast","z":"a0a2199f.f5e638","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"","topic":"fallo","name":"fallo","x":1630,"y":640,"wires":[[]]},{"id":"356fac5d.d980c4","type":"ui_group","z":"","name":"Pacientes","tab":"16e86f12.023081","order":2,"disp":true,"width":"6","collapse":true},{"id":"698ecd0.1670e34","type":"MySQLdatabase","z":"","host":"","port":"","db":"","tz":""},{"id":"16e86f12.023081","type":"ui_tab","z":"","name":"dispositivo","icon":"healing","order":2}]

```

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [8 February 2019 14:31 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/22 "2019-02-08T14:31:32Z")

</div>

1)I have the error in node function "insert-query".  
2) In this insert the data of the table (name, surname, age, identity card, sex)

```auto
insert = {topic: "INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('" + msg.payload.name + "','" + msg.payload.apellido + "','" + msg.payload.cedula + "','" + msg.payload.edad + "','" + msg.payload.sexo + "')"};
actionString = "Dato ingresado satisfactoriamente!";
return [insert,actionString];

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [8 February 2019 14:35 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/23 "2019-02-08T14:35:51Z")

</div>

So add a debug after that and what is the message you are sending?

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [8 February 2019 14:45 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/24 "2019-02-08T14:45:01Z")

</div>

I always have this message when I enter a "name" already stored in the table or a new "name".  
This I get at the exit of the node function "insert-query".  
 ![INSERT1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b734686ea4eee03141ad77e12496b9993396e981.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [8 February 2019 14:52 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/25 "2019-02-08T14:52:48Z")

</div>

So if it isn't working you need to break the problem down to fid the bit that is broken.  
Check that the message going into the function you posted

```auto
insert = {topic: "INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('" + msg.payload.name + "','" + msg.payload.apellido + "','" + msg.payload.cedula + "','" + msg.payload.edad + "','" + msg.payload.sexo + "')"};
actionString = "Dato ingresado satisfactoriamente!";
return [insert,actionString];

```

and the messages that leave the function are what you think they are. You do this by adding debug nodes. If it can't read property 'name' check that where you are trying to use `name` it exists.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 February 2019 15:00 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/26 "2019-02-08T15:00:19Z")

</div>

As @ukmoose said, you need to work backwards. If your 'insert-query' is getting an error, you need to look at what is going into that node. Looking at your flow the input comes from the switch node and the msg that enters the switch node comes from the 'select-query' function.

So what is coming out of that node and is it the data you are trying to access in the 'insert-query' node?

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [8 February 2019 15:20 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/27 "2019-02-08T15:20:29Z")

</div>

I appreciate your help @ukmoose , @zenofmud

I get the following:

1. Debug: node name:  
 ![name](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6b69d8df157dada0c93db47a3ea4f7d76502eac8.png)

2. Debug output: node join:  
 ![node_join](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6645ea8e99b2242831dcf9537c7790232130949e.png)

3. Debug: switch\_select  
 ![node_switch1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/527e87d262a6fafb67028a3ed657b118966c48a2.png)

4. The error is presented in the node function "select-query", because I have a "msg.payload: undefined".  
 ![node_select](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cf3e49caa72670de60913afc49aaf39548f003a9.png)

5. Debug: node\_switch 2; this is the output of the switch\_insert  
 ![salida_s2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/be0b1c7cedecf20599e6694f5de3f6dc8fedd38d.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 February 2019 15:50 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/28 "2019-02-08T15:50:56Z")

</div>

So your select-query function has an error saying that msg.payload is not defined. Take a step back and figure out where the data send to that function is coming from and what is in the msg being sent.

It is your responsibility to send data to that function node in a format that the function node will use.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [8 February 2019 16:18 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/29 "2019-02-08T16:18:06Z")

</div>

The data comes from the "switch\_select" node, and I get the following:  
 ![sw1_i3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/58dc199241f46c4ba316002b3d498ac5bb6c4cf1.png)

I have verified the path of the name message and it is this:  
payload.name

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 February 2019 16:20 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/30 "2019-02-08T16:20:28Z")

</div>

```auto
insert = {topic: "INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('" + msg.payload.name + "','" + msg.payload.apellido + "','" + msg.payload.cedula + "','" + msg.payload.edad + "','" + msg.payload.sexo + "')"};
actionString = "Dato ingresado satisfactoriamente!";
return [insert,actionString];

```

In this code you are returning the `insert` message on the first output of the Function node and you are returning `actionString` on the second output of the Function node.

**But** `actionString` is a string - not a message object. You must return messages from the Function, not plain strings.

```auto
var insert = {topic: "INSERT INTO pacientes (nombre,apellido,cedula,edad,sexo) VALUES ('" + msg.payload.name + "','" + msg.payload.apellido + "','" + msg.payload.cedula + "','" + msg.payload.edad + "','" + msg.payload.sexo + "')"};
var actionString = {payload:"Dato ingresado satisfactoriamente!"};
return [insert,actionString];

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 February 2019 16:55 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/31 "2019-02-08T16:55:22Z")

</div>

@knolleary Nick, that is the second error  
@Tefita In the 'select query' function you are returning an object with only a msg.topic and no msg.payload. That is why you are getting the `msg.payload: undefined`

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 February 2019 17:22 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/32 "2019-02-08T17:22:55Z")

</div>

> [@zenofmud](#):
>
> @Tefita In the 'select query' function you are returning an object with only a msg.topic and no msg.payload. That is why you are getting the `msg.payload: undefined`

And that probably isn't an error, because the sql node uses msg.topic to define the query and doesn't use msg.payload.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 February 2019 17:39 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/33 "2019-02-08T17:39:22Z")

</div>

At least cleaning that up will be a good learning exercise for @Tefita as she trys to learn how to use NR.

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [9 February 2019 03:40 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/34 "2019-02-09T03:40:31Z")

</div>

I have done the mentioned thing but now I have another error:

```auto
var select = {payload: "SELECT COUNT(*) AS count FROM pacientes WHERE nombre='" + msg.payload.name + "'"};
return select;

```

![SELECT1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/745b5d0578ac3de78be2e8abb25002fb34a205ed.png)

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [9 February 2019 03:42 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/35 "2019-02-09T03:42:13Z")

</div>

Then should consider what was said by @knolleary : sql node uses msg.topic

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 February 2019 10:27 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/36 "2019-02-09T10:27:39Z")

</div>

So by this last post of yours, can we assume you have it working?

---

<div class="post-metadata">

**Author:** ![Tefita](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@Tefita](https://discourse.nodered.org/u/Tefita)\
**Post date:** [10 February 2019 00:20 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/37 "2019-02-10T00:20:38Z")

</div>

No, I need your help please, I continue with the problem, I do not know how to solve it.  
This error in is node function "select-query"  
 ![select2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/020614e2ea94f943f46b0e8751411cb1235a9c04.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 February 2019 03:02 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/38 "2019-02-10T03:02:48Z")

</div>

As I said before look at the msg entering the function node. In the function node you are trying to get information out of the payload but there is no payload. You have to fix that!

---

<div class="post-metadata">

**Author:** ![toshniwalrup](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@toshniwalrup](https://discourse.nodered.org/u/toshniwalrup)\
**Post date:** [24 August 2022 18:24 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/39 "2022-08-24T18:24:13Z")

</div>

Hi,  
I'm also getting same problem  
SELECT COUNT(_) AS count FROM scanner WHERE scannerdata='a' : msg.payload : undefined  
[  
{  
"id": "b727ae2d17cd20f9",  
"type": "tab",  
"label": "Flow 6",  
"disabled": false,  
"info": "",  
"env": []  
},  
{  
"id": "161724188b20085f",  
"type": "ui\_text\_input",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"label": "",  
"tooltip": "",  
"group": "21fe878da3854fff",  
"order": 10,  
"width": 0,  
"height": 0,  
"passthru": false,  
"mode": "text",  
"delay": "0",  
"topic": "payload",  
"sendOnBlur": false,  
"className": "",  
"topicType": "msg",  
"x": 180,  
"y": 1000,  
"wires": [  
[  
"41989a70fc4eceed",  
"32bb7ff6b2b4f975"  
]  
]  
},  
{  
"id": "1d79023b9138099f",  
"type": "debug",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"active": false,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "payload",  
"targetType": "msg",  
"statusVal": "",  
"statusType": "auto",  
"x": 530,  
"y": 980,  
"wires": []  
},  
{  
"id": "e2381e9472463e2f",  
"type": "change",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"rules": [  
{  
"t": "set",  
"p": "payload",  
"pt": "msg",  
"to": "",  
"tot": "str"  
}  
],  
"action": "",  
"property": "",  
"from": "",  
"to": "",  
"reg": false,  
"x": 320,  
"y": 1200,  
"wires": [  
[  
"161724188b20085f"  
]  
]  
},  
{  
"id": "32bb7ff6b2b4f975",  
"type": "delay",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"pauseType": "delay",  
"timeout": "1",  
"timeoutUnits": "seconds",  
"rate": "1",  
"nbRateUnits": "1",  
"rateUnits": "second",  
"randomFirst": "1",  
"randomLast": "5",  
"randomUnits": "seconds",  
"drop": false,  
"allowrate": false,  
"outputs": 1,  
"x": 120,  
"y": 1200,  
"wires": [  
[  
"e2381e9472463e2f"  
]  
]  
},  
{  
"id": "41989a70fc4eceed",  
"type": "function",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"func": "if(msg.payload || 0)\n{\nglobal.set('scannerread',msg.payload);\nreturn msg;\n}",  
"outputs": 1,  
"noerr": 0,  
"initialize": "",  
"finalize": "",  
"libs": [],  
"x": 340,  
"y": 1000,  
"wires": [  
[  
"1d79023b9138099f",  
"eafb48b2b763c2c8",  
"0367dc0573d03a3c"  
]  
]  
},  
{  
"id": "eafb48b2b763c2c8",  
"type": "ui\_text",  
"z": "b727ae2d17cd20f9",  
"group": "21fe878da3854fff",  
"order": 10,  
"width": 0,  
"height": 0,  
"name": "",  
"label": "Last scanned barcode",  
"format": "{{msg.payload}}",  
"layout": "row-spread",  
"className": "",  
"x": 580,  
"y": 1040,  
"wires": []  
},  
{  
"id": "0367dc0573d03a3c",  
"type": "function",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"func": "var z=global.get('scannerread');\nmsg.payload=z;\n//msg.payload=z;\n//msg.payload={topic:"SELECT scannerdata,count(scannerdata = '"+z+"') FROM training.scanner;"};\n//var select = {topic: "SELECT COUNT(_) AS count FROM test WHERE name='" + msg.payload.name + "'"};\n\nvar select = {topic: "SELECT COUNT(\*) AS count FROM scanner WHERE scannerdata='" + msg.payload + "'"};\nreturn select;",  
"outputs": 1,  
"noerr": 0,  
"initialize": "",  
"finalize": "",  
"libs": ,  
"x": 540,  
"y": 1260,  
"wires": [  
[  
"72f8055014c1dc6e",  
"724a9f33193410af",  
"ad9ac705413818c1"  
]  
]  
},  
{  
"id": "72f8055014c1dc6e",  
"type": "mysql",  
"z": "b727ae2d17cd20f9",  
"mydb": "7e23b89935e72915",  
"name": "",  
"x": 780,  
"y": 1240,  
"wires": [  
  
]  
},  
{  
"id": "724a9f33193410af",  
"type": "debug",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"active": true,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "true",  
"targetType": "full",  
"statusVal": "",  
"statusType": "auto",  
"x": 730,  
"y": 1320,  
"wires":   
},  
{  
"id": "69e9418553ca469a",  
"type": "debug",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"active": false,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "true",  
"targetType": "full",  
"statusVal": "",  
"statusType": "auto",  
"x": 690,  
"y": 1400,  
"wires":   
},  
{  
"id": "6cb5f64655eaf652",  
"type": "function",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"func": "actionString = "Data insertion failed!";\nmsg.payload = actionString;\nreturn msg;\n",  
"outputs": 1,  
"noerr": 0,  
"initialize": "",  
"finalize": "",  
"libs": ,  
"x": 640,  
"y": 1480,  
"wires": [  
[  
"fa3bbe7bcccb4548"  
]  
]  
},  
{  
"id": "fa3bbe7bcccb4548",  
"type": "debug",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"active": false,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "false",  
"statusVal": "",  
"statusType": "auto",  
"x": 790,  
"y": 1480,  
"wires":   
},  
{  
"id": "ad9ac705413818c1",  
"type": "switch",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"property": "payload[0].count",  
"propertyType": "msg",  
"rules": [  
{  
"t": "eq",  
"v": "0",  
"vt": "num"  
},  
{  
"t": "else"  
}  
],  
"checkall": "true",  
"repair": false,  
"outputs": 2,  
"x": 450,  
"y": 1440,  
"wires": [  
[  
"69e9418553ca469a"  
],  
[  
"6cb5f64655eaf652",  
"d30d854110c2756d"  
]  
]  
},  
{  
"id": "d30d854110c2756d",  
"type": "debug",  
"z": "b727ae2d17cd20f9",  
"name": "",  
"active": true,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "false",  
"statusVal": "",  
"statusType": "auto",  
"x": 470,  
"y": 1520,  
"wires":   
},  
{  
"id": "21fe878da3854fff",  
"type": "ui\_group",  
"name": "Export Data",  
"tab": "750a8b0a2f1e55a8",  
"order": 3,  
"disp": false,  
"width": "6",  
"collapse": false,  
"className": ""  
},  
{  
"id": "7e23b89935e72915",  
"type": "MySQLdatabase",  
"name": "",  
"host": "127.0.0.1",  
"port": "3306",  
"db": "training",  
"tz": "",  
"charset": "UTF8"  
},  
{  
"id": "750a8b0a2f1e55a8",  
"type": "ui\_tab",  
"name": "Export",  
"icon": "dashboard",  
"disabled": false,  
"hidden": false  
}  
]

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 August 2022 19:34 UTC](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754/40 "2022-08-24T19:34:45Z")

</div>

In order to make flow or code importable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

However, if the problem is that you are having difficulty getting the right query, and you are creating the query in a function node, then just post the code of the function node, and show us what is going _into_ the function node, and what is coming out of it. There is no need to post a large flow.

[Previous page](https://discourse.nodered.org/t/how-to-check-whether-there-is-existing-data-before-allowing-users-to-insert-the-data-to-the-database/1754.md?page=1)
