# Sir ,I want to copy 2 msg.id in my sql DB

**URL:** https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912
**Category:** General
**Created:** [15 September 2020 12:12 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912 "2020-09-15T12:12:07Z")
**Posts on this page:** 20
**Page:** 5

<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 September 2020 14:26 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/81 "2020-09-17T14:26:48Z")

</div>

I increased the delay upto 10 sec now check the output for  
Case 3:

```auto
9/17/2020, 7:55:50 PMnode: dcb68435.c57928
Trade_ID : msg.payload : Object
object
Fob_Tag_ID: "04222436"
Trade_ID: "2814"

```

---

<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 September 2020 14:39 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/82 "2020-09-17T14:39:28Z")

</div>

What do _you_ think? This is _your_ application and _you_ need to understand it.

* * *

So yes, that is looking good.

The next task is to create a Switch node that handles the three cases.

Have told you to use a Switch node repeatedly, the truth is the logic you need is actually a bit tricky to express in the Switch node. It will be easier to use a Function node.

Replace that Switch node with a Function node. Configure the Function node to have 3 outputs and use this code:

```auto
var hasTag = msg.payload.hasOwnProperty("Fob_Tag_ID");
var hasTrade = msg.payload.hasOwnProperty("Trade_ID");

if (hasTag && !hasTrade) {
   return [msg, null, null];
} else if (!hasTag && hasTrade) {
   return [null, msg, null];
} else if (hasTag && hasTrade) {
   return [null, null, msg];
}

```

I hope that code is self-explanatory.

---

<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 September 2020 14:46 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/83 "2020-09-17T14:46:42Z")

</div>

Sir i have replaced the switch node with this function you just gave  
output of this function node acc to case 1:

Case1:

```auto
9/17/2020, 8:14:37 PMnode: function_debug
Fob_Tag_ID : msg : Object
object
topic: "Fob_Tag_ID"
_msgid: "45329283.c1930c"
payload: object
Fob_Tag_ID: "04222436"

```

---

<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 September 2020 14:48 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/84 "2020-09-17T14:48:53Z")

</div>

You don't need to show me the output of the Function because the Function doesn't _modify_ the message at all.

All the Function does is pass the message to one of its three outputs according to which case it is for. _That_ is what you now need to verify: add a Debug node to each output of the Function and make sure that Case 1 appears on the top output of the Function, Case 2 from the middle output and Case 3 from the bottom output.

If that is all working, you can then add whatever logic you want in each of those separate cases.

---

<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 September 2020 14:53 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/86 "2020-09-17T14:53:34Z")

</div>

> [@knolleary](#):
>
> _That_ is what you now need to verify: add a Debug node to each output of the Function and make sure that Case 1 appears on the top output of the Function, Case 2 from the middle output and Case 3 from the bottom output.

All 3 output is coming from that function ,now my sql query which i have to write for case 3 especially what it could be?

---

<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 September 2020 14:55 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/87 "2020-09-17T14:55:32Z")

</div>

> [@shipra](#):
>
> All 3 output is coming from that function

You need to be clear. Are you seeing the messages get sent on the correct output of the Function to match each case?

> [@shipra](#):
>
> sql query which i have to write for case 3 especially what it could be?

You want an INSERT statement with both values in right? You already know how to do an insert with one value - so why don't you try writing the query yourself. If you get stuck, share what you have tried and we can help.

---

<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 September 2020 15:06 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/88 "2020-09-17T15:06:09Z")

</div>

Sir Case 1 and 2 i am getting output in correct mannner

but for case 3 check:

```auto
9/17/2020, 8:34:35 PMnode: Fob&tagINSERT INTO NCS_DATABASE2 (Date_and_Time_India,Date_and_Time_UK ,Event_Type ,Event_Description ,Person_ID,Person_Name_Description ,Fob_Tag_ID ,Trade_ID,Door_ID ,Door_Description ,Status) VALUES ((strftime('%Y-%m-%d %H:%M:%S','now','localtime')),(strftime('%Y-%m-%d %H:%M:%S','now','localtime','-4 hour','-30 minutes')),'FOB & TAG','DR OPEN BY FOB & TAG',' ','Shipra Nigam',' ','2814','2814 ',' ','High Risk DR ON') : msg : Object

```

2814 is getting stored twice.please suggest

---

<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 September 2020 15:11 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/89 "2020-09-17T15:11:54Z")

</div>

How can I suggest anything? You have not shown how you are generating the SQL.

---

<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 September 2020 15:13 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/90 "2020-09-17T15:13:37Z")

</div>

Pardon Sir,

query for 3rd case looks like this ,this i have done previously so modification is required ,kindly suggest

```auto
let newMsg2 = {};

newMsg2.topic = `INSERT INTO NCS_DATABASE2 (Date_and_Time_India,Date_and_Time_UK ,Event_Type ,Event_Description ,Person_ID,Person_Name_Description ,Fob_Tag_ID ,Trade_ID,Door_ID ,Door_Description ,Status)
VALUES ((strftime('%Y-%m-%d %H:%M:%S','now','localtime')),(strftime('%Y-%m-%d %H:%M:%S','now','localtime','-4 hour','-30 minutes')),'FOB & TAG','DR OPEN BY FOB & TAG',' ','Shipra Nigam',' ','${msg.id}','${msg.id1} ',' ','High Risk DR ON')`;

return newMsg2;

```

---

<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 September 2020 15:16 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/91 "2020-09-17T15:16:00Z")

</div>

So your query is using the message properties `msg.id` and `msg.id1`. But we have spent the whole of today getting to the point where `msg.payload.Fob_Tag_ID` contains the fob and `msg.payload.Trade_ID` contains the trade id.

Can you see how to change your code?

---

<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 September 2020 15:24 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/92 "2020-09-17T15:24:09Z")

</div>

thank you and sorry for my silly mistake i removed that and getting the three output correctly.

but sir one query is ,if i am entering for case 3:  
Fob id :000000000(wrong id)  
Tag id : 2814(correct id)  
then why it is updating me a case 2 Sql query????? this is not correct?please suggest

---

<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 September 2020 15:26 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/93 "2020-09-17T15:26:49Z")

</div>

Again, how can I possibly suggest anything as I am not sat in front of your flow?

Can you explain what you mean in terms of all the work we have done?

In the scenario you are asking about, which of the three outputs of the Function node is the message passing? What does that message look like exactly?

At this point, you really should be in a position to debug these things yourself. We've have spent so much time stepping through the logic in your flow and building it up step by step.

Use Debug nodes to understand where messages are going in your flow. Figure it out.

---

<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 September 2020 15:33 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/94 "2020-09-17T15:33:21Z")

</div>

Yes sir , like as i am entering 00000000(wrong id) and 2814 (right id) ,then in output of my 2 function node is coming .it should not come because user has entered first id wrong ,in this case no updation would be there (whether first id or second id wrong then no updation )

---

<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 September 2020 15:34 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/95 "2020-09-17T15:34:01Z")

</div>

this is for when i am in case 3 and entering one wrong id and right id

---

<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 September 2020 15:41 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/96 "2020-09-17T15:41:58Z")

</div>

Ah okay. That makes sense.

At the moment, your flow checks the Tag/Trade are valid values and only passes them to the Join node if they are valid.

The problem with that approach is exactly what you are seeing: if it is scenario 3 (tag & trade provided) but either one of them is invalid, then the Join node will pass on a message with only the valid value - so the Function node thinks it is case 1 or case 2.

There are a few ways you could fix it. One way would be, earlier in the flow, when you identify it is an invalid value, then set it to something like `INVALID`, but still pass it on to the Join node. That means you will be sure to get "complete" messages in all three cases. You would then add some additional logic in the Function node after the Join node to check to see if the value is valid or not.

Something like:

```auto
var hasTag = msg.payload.hasOwnProperty("Fob_Tag_ID") && msg.payload.Fob_Tag_ID !== "INVALID";
var hasTrade = msg.payload.hasOwnProperty("Trade_ID") && msg.payload.Trade_ID !== "INVALID";

```

---

<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 September 2020 15:51 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/97 "2020-09-17T15:51:23Z")

</div>

> [@knolleary](#):
>
> when you identify it is an invalid value, then set it to something like `INVALID` , but still pass it on to the Join node.

Sir all you said i got that but this line can u please elaborate please

---

<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 September 2020 15:58 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/98 "2020-09-17T15:58:58Z")

</div>

like this i tried ..  
 ![g](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/43c83004c6570a45d149502f76ad71c90c2bab74.png)

please suggest

---

<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 September 2020 16:30 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/99 "2020-09-17T16:30:59Z")

</div>

![kk](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/0546bf3011d5a903ae6c592c567a20eda6eb8270.png)

like this i have done acc to what you suggest ,litlle bit not getting the correct output,please suggest.

 ![k](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a591d7d3cc5f5eb3858ce3c06e05c16168a5781f.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 September 2020 17:02 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/100 "2020-09-17T17:02:33Z")

</div>

> [@shipra](#):
>
> bit not getting the correct output,please suggest.

Once again, how can I help if you don't say what is wrong? You say you don't get the correct output, but you don't say **what** output you get. You have shared a screenshot of some Change nodes, but no information on how you have configured them.

At a glance, I can see you are using `msg.payload1` in the Function code. Where has that come from?

---

<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: [21 September 2020 09:21 UTC](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912/101 "2020-09-21T09:21:25Z")

</div>

Sir ,I want to add a refresh button in ui dashboard for this project ,by this button user can get the recent updated database .and further i want to get this updated data back to mqtt sub node with come string lets say "database successfully updated" on subscriber side .How can i implement this .  
I am arising this query here because I dont want to explain my project from any new person ,so that he also dont get in trouble and he can understand my flow easily.

[Previous page](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912.md?page=4)

[Next page](https://discourse.nodered.org/t/sir-i-want-to-copy-2-msg-id-in-my-sql-db/32912.md?page=6)
