# Connection with database error

**URL:** <https://discourse.nodered.org/t/connection-with-database-error/30172>\
**Category:** General\
**Created:** [16 July 2020 06:36 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172 "2020-07-16T06:36:54Z")\
**Posts on this page:** 20\
**Page:** 5

<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:** [17 July 2020 12:02 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/82 "2020-07-17T12:02:42Z")

</div>

Well, don't do that. No one said to do that.

You want the switch node exactly as @zenofmud provided.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:05 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/83 "2020-07-17T12:05:04Z")

</div>

ok sorry.ya done ,so thats a switch ok ![l](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e2c9dc53e4a731ba8bc26c412e6f4cbb43f540ef.png)

---

<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:** [17 July 2020 12:10 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/84 "2020-07-17T12:10:31Z")

</div>

So what do you think you need to do with the Switch node?

You haven't connected it to anything there so it won't do much like that.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:11 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/85 "2020-07-17T12:11:23Z")

</div>

![db](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cc8f3ff85683bcd624fca9494fad4eaba6c24819.png)  
SEE LIKE THIS

---

<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:** [17 July 2020 12:13 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/86 "2020-07-17T12:13:19Z")

</div>

There's no question there, so I assume you know what to do next?

I assume you've now tested it to see what message you get from each of its outputs?

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:15 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/87 "2020-07-17T12:15:47Z")

</div>

![r](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/877391b72f39b8146ad994ef3a0e5f39092b80a2.png)  
see ones.

---

<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:** [17 July 2020 12:19 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/88 "2020-07-17T12:19:07Z")

</div>

Sorry, but I'm not sure what you want me to look at in that screenshot

Have you tested it? Is it working?

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:21 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/89 "2020-07-17T12:21:02Z")

</div>

![n](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5999365128442feac691e878d8881c9bf274ec0c.png)  
check this

---

<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:** [17 July 2020 12:22 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/90 "2020-07-17T12:22:29Z")

</div>

What do you want me to check? I know nothing about your application, I don't know what that screenshot is showing me.

It is a simple question - do you get a message from the `Switch_debug` node when you enter a valid ID?

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:23 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/91 "2020-07-17T12:23:45Z")

</div>

when i am entering valid id it is only showing the id,but when i am entering invalid id it is showing me empty

---

<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:** [17 July 2020 12:27 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/92 "2020-07-17T12:27:05Z")

</div>

If you look at the Switch node's configuration, you can see it has a rule to check if `msg.payload` is empty - and that will cause a message to be sent on the first output.

It's second rule is to check if it is not empty - and that will cause a message to be sent on the second output.

You have wired up the first output of the Switch node, so you will only get a message there if the ID doesn't match anything in the database.

If you want to handle messages that _do_ match something in the database, use the second output of the Switch node.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:30 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/93 "2020-07-17T12:30:05Z")

</div>

![k](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8cbf108e3fd0619cf2d0993c7558b40169971dfe.png)  
ya now check ones.

---

<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:** [17 July 2020 12:32 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/94 "2020-07-17T12:32:01Z")

</div>

You have named both debug nodes `Switch_debug` - so from a static screenshot it's impossible to tell which message came from which node.

But you don't need me to tell you if its working - you should be able to see for yourself what happens when you inject a valid or invalid message.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:35 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/95 "2020-07-17T12:35:09Z")

</div>

![v](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/114b4741b0b8e92962ae06103ac64ccab9058a03.png)

YES SIR ,I want a string to be display there not empty(i want a CR\_FAIL) string in case of invalid and (i want CR\_SUCCESS) in case of valid.

---

<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:** [17 July 2020 12:37 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/96 "2020-07-17T12:37:04Z")

</div>

You can use the Change node to set the payload of the message to whatever value you want.

Now the Switch node has done its job of separating out the invalid and valid messages, you can add one Change node wired to the top output to set the payload to `CR_FAIL` and one Change node wired to the bottom output to set the payload to `CR_SUCCESS`.

As before, add your Debug nodes _after_ the Change nodes so you can check what it is doing.

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:43 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/97 "2020-07-17T12:43:54Z")

</div>

Ya now its working

 ![kk](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/eec98fb2b98bc18bd54d3aa45e846ada0d86fb33.png)  
Thank you .  
Now Sir I want this CR\_SUCCESS and CR\_FAIL send to my mqtt out for subscriber part  
How to do that.

---

<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:** [17 July 2020 12:45 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/98 "2020-07-17T12:45:38Z")

</div>

> [@shipra](#):
>
> Now Sir I want this CR\_SUCCESS and CR\_FAIL send to my mqtt out for subscriber part  
> How to do that.

At this point, given what you've learnt so far, can you make a guess as how you might do that?

Hint - what node might you use to publish a message to an MQTT topic?

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:46 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/99 "2020-07-17T12:46:59Z")

</div>

yes definitely , but sir one issue whenever i am passing E1234562 which is is my database it is showing CR\_FAIL why

---

<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:** [17 July 2020 12:52 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/100 "2020-07-17T12:52:03Z")

</div>

Do you understand how the flow is meant to be working? Do you understand what each node is meant to be doing?

If something is not behaviour as you think it should, you can use the Debug nodes throughout the flow to see exactly what is happening.

So start with: what does the mysql node return when you pass in `E1234562`?

---

<div class="post-metadata">

**Author:** ![shipra](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@shipra](https://discourse.nodered.org/u/shipra)\
**Post date:** [17 July 2020 12:54 UTC](https://discourse.nodered.org/t/connection-with-database-error/30172/101 "2020-07-17T12:54:08Z")

</div>

> [@knolleary](#):
>
> start with: what does the mysql node return when you pass in `E1234562` ?

 ![a](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1dfbec4701861fc0a609e2442d011ea3a20200d4.png)

[Previous page](https://discourse.nodered.org/t/connection-with-database-error/30172.md?page=4)

[Next page](https://discourse.nodered.org/t/connection-with-database-error/30172.md?page=6)
