# ODBC connector, multiple msg(SQL) from For cycle

**URL:** <https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080>\
**Category:** Jobs\
**Tags:** database\
**Created:** [16 October 2023 11:06 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080 "2023-10-16T11:06:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 11:06 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/1 "2023-10-16T11:06:23Z")

</div>

Hello evrey one,

have an issue with the Helix ODBC node. I'm passing three messages from function node with three different queries (tested and correct), but it returns three identical messages related to the results of the last received query. Could it be a cache issue?

thanks

---

<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:** [16 October 2023 11:29 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/2 "2023-10-16T11:29:07Z")

</div>

Welcome to the forum @Riccaleu

The most likely cause of this is that you are re-using the same msg object in your function. In javascript objects are stored by reference, so if you do, for example,

```auto
msg.payload = "something"
node.send(msg)
msg.payload = "something else"
node.send(msg)

```

then the second `msg.payload =` statement will overwrite the payload of the message you have just sent, which the ODBC node will not yet have actioned.  
One solution is to clone the existing message each time

```auto
msg1 = RED.util.cloneMessage(msg)
msg1.payload = "something"
node.send(msg1)
msg2 = RED.util.cloneMessage(msg)
msg2.payload = "something else"
node.send(msg2)

```

You won't need to clone for the third one.

If that isn't it then post the code of the function. Copy/paste please, not screenshot.

See also [Cloning messages in a flow : Node-RED](https://nodered.org/blog/2019/09/13/cloning-messages) which goes into cloning and pass by reference in more detail.

---

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 11:44 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/3 "2023-10-16T11:44:16Z")

</div>

Thanks for the quick reply

i have a message for each i element could be thousand

here the code

```auto
var query = ""
for (var i = 0; i < length; i++) {

     query = 
     `SELECT 
        
     var msg = {}
     msg.query = query
 
     msg.dsn = global.get("dsn");
     node.send(msg);

} 

```

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 12:09 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/4 "2023-10-16T12:09:39Z")

</div>

Switch to using `let` or not use a key identifier like `msg` along with `var`

`var` will apply to outside the scope - and if you use `msg` with `var` - you are affecting the `msg` that is still travelling during the cycle

```auto
for (let i = 0; i < length; i++) {
     
      let query = `SELECT...'
      let msg = {}
      msg.query = query

      msg.dsn = global.get("dsn");
      node.send(msg);
}

```

Examples

```auto
var msg = {"Hello": "World"}

for(var i = 0; i<10;i++){
    var msg = {"What": "The!"}
    node.send(msg)
}
// msg is now surprisingly {"What": "The!"}

```

```auto
var msg = {"Hello": "World"}

for(let = 0; i<10;i++){
    let msg = {"What": "The!"}
    node.send(msg)
}
// msg is still {"Hello": "World"}

```

---

<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:** [16 October 2023 12:16 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/5 "2023-10-16T12:16:14Z")

</div>

Which is one of the reasons why var is now deprecated. Use let or const instead.

---

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 12:25 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/6 "2023-10-16T12:25:12Z")

</div>

Thanks for the suggestion,  
i applied let for each variable but unfortunately nothing change

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 12:34 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/7 "2023-10-16T12:34:28Z")

</div>

can you share whole `function` block?

---

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 12:39 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/8 "2023-10-16T12:39:14Z")

</div>

yes , for privacy i need to mask some stuff`

```auto
context.data = context.data || {};

switch (msg.topic) {
     case "string":
          context.data.string = msg.payload.items;
          msg = null;
          break; 
}

let IdWL = context.data.string

let dateStart = new Date();
dateStart.setMinutes(0);
dateStart.setSeconds(0);//arrotondo alll'ora intera corrente

let dateEnd = new Date();

dateEnd.setDate(dateEnd.getDate() - 5)
dateEnd.setHours(0);
dateEnd.setMinutes(0);
dateEnd.setSeconds(0);

let dataStartFormat = dateStart.toISOString().replace('T', ' ').substring(0, 19);
let dataEndFormat = dateEnd.toISOString().replace('T', ' ').substring(0,19);

for (let i = 0; i < IdWL.length; i++) {

     let msg ={}
     let query = 
     `SELECT 
          ID,RECORDTIME, VALUE
     FROM 
          HIS15MIN
     WHERE 
         
     ORDER BY
          "RecordTime" ASC`
     
    
     msg.query = query
     msg.toipic = IdWL[i].pointId
     msg.dsn = global.get("dsn");
     node.send(msg);

}

```

Thanks a lot

---

<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:** [16 October 2023 13:16 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/9 "2023-10-16T13:16:10Z")

</div>

What happens when you rate limit the output of the function node using a delay node set to rate limit? giving enough time to run each query.

---

<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:** [16 October 2023 13:33 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/10 "2023-10-16T13:33:47Z")

</div>

> [@Riccaleu](#):
>
> ` msg.toipic = IdWL[i].pointId`

The spelling there looks suspect.

As well as rate limiting as suggested, feed the output of the function node into a debug node set to Output Complete Message and check that they look correct.

---

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 13:37 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/11 "2023-10-16T13:37:41Z")

</div>

**Thanks It works!**

but i don't think is the best way to solve the **problem** , at least not the most efficient one, i'd like to menage it trougth waiting the message to complete its path or something like this

---

<div class="post-metadata">

**Author:** ![Riccaleu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riccaleu/32/83807_2.png) [@Riccaleu](https://discourse.nodered.org/u/Riccaleu)\
**Post date:** [16 October 2023 13:38 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/12 "2023-10-16T13:38:20Z")

</div>

sorry,  
just an error of writing. results in debug are ok!

---

<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:** [16 October 2023 13:41 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/13 "2023-10-16T13:41:32Z")

</div>

knowing nothing about the node forward makes it hard to help further.

You can use rate limit as a queue. To make it more efficient once your sql node responds you can feed msg.flush set to 1 back to the delay node to release next. Read help text for delay node, both Colin and me have posted example flows for this in the past.  
here is one

> [@Problem with TCP requests](https://discourse.nodered.org/t/problem-with-tcp-requests/81538/9):
>
> Will queuing the inputs and only allowing new input once tcp responds work [{"id":"7f219818a234a3c6","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"eeeeee","payloadType":"str","x":510,"y":520,"wires":[["3b447fd41df4372d"]]},{"id":"3b447fd41df4372d","type":"function","z":"d1395164b4eec73e","name":"add \"\\r\\n\"","func":"msg.payload = msg.payload + '\\r\\n';\nreturn ms…

> [@How to process any messaging queues synchronously in node-red](https://discourse.nodered.org/t/how-to-process-any-messaging-queues-synchronously-in-node-red/71805/7):
>
> If you mean that you have a section of flow and you want to wait till one message has completely passed through that section before the next one is allowed through then have a look at this example of how to do it. Set the time in the Queue node to a greater time than your flow should ever take to run through. If something locks up then it will release the next message after that time in order to let things keep going. [{"id":"b6630ded2db7d680","type":"inject","z":"bdd7be38.d3b55","n…

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [16 October 2023 13:58 UTC](https://discourse.nodered.org/t/odbc-connector-multiple-msg-sql-from-for-cycle/82080/14 "2023-10-16T13:58:43Z")

</div>

Just to add some input, which I think is related??

a Semaphore node that was discussed above, had some issues, so I addressed them with my take on a Semaphore Node (with a lot of extras)

It may come in handy to address the discussed

> **[@marcus-j-davies/node-red-semaphore-plus](https://flows.nodered.org/node/@marcus-j-davies/node-red-semaphore-plus)**
>
> An advanced set of semaphore Nodes for Node RED, with a dynamic fail-safe
