# Loop in function node don´t send all topics into sqlite database

**URL:** <https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230>\
**Category:** General\
**Created:** [23 September 2020 13:30 UTC](https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230 "2020-09-23T13:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZsomborPiros](https://avatars.discourse-cdn.com/v4/letter/z/ecae2f/32.png) [@ZsomborPiros](https://discourse.nodered.org/u/ZsomborPiros)\
**Post date:** [23 September 2020 13:30 UTC](https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230/1 "2020-09-23T13:30:29Z")

</div>

Hi, i want to safe key-value pairs in a database. Because i dont found some options i integrated sqlite in my flow. My problem is the function node doesn´t send all topics. When i inject i should have 111 entries but it is always like 20 random entries from the pool. I have to inject like 30 times to get the whole pool of key-value pairs in my sqlite table. I think the messages are sent to fast, so it cant be safed. Can someone tell me how i can fix this issue?

This is my funcion node:

```auto
var b= Buffer.from(msg.payload);

if(b[0]== 0x7c && b[1]== 0x4 && b[2]== 0x4 && b[3] == 0x60){ return null;
}else{
    
for(i =0; i< b.length ; i++){
    if(b[i] == 0x7c && b[i+1] == 0x06){
      
       var busnummer = b[i+4];
       var wert = b[i+5];
        
     
    var topic="INSERT INTO BUS(BUSNUMMER,WERT) values("+"\'"+busnummer+"\'"+","+"\'"+wert+"\'"+")";
    msg.topic=topic;
      return msg
    }
} 

```

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c06f8d5fcc3ce7a99977c914af32edaa7a4c1ca2.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:** [23 September 2020 13:33 UTC](https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230/2 "2020-09-23T13:33:25Z")

</div>

The problem is due to the fact you have the `return` statement in the middle of the loop.

That will exit the function as soon as it reaches that point - so your loop never loops more than once.

As you want to send multiple messages, you should use `node.send()` inside the loop, instead of `return`:

```auto
var newMsg = { topic: "INSERT INTO BUS(BUSNUMMER,WERT) values("+"\'"+busnummer+"\'"+","+"\'"+wert+"\'"+")" }
node.send(newMsg);

```

---

<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:** [23 September 2020 20:08 UTC](https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230/3 "2020-09-23T20:08:53Z")

</div>

Don't you also want an `i=i+5` after the `node.send()` to stop it checking all the bytes you have just picked up data from?

---

<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:** [7 October 2020 20:08 UTC](https://discourse.nodered.org/t/loop-in-function-node-don-t-send-all-topics-into-sqlite-database/33230/4 "2020-10-07T20:08:54Z")

</div>

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