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

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:

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
    }
} 

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:

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

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?

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