Duplicated the payload twice at once

I am stuck with the flow that shown below, its read from DB and then does checking process by using If statement, then displays the output. The problem is that it's duplicated the message payload each time injected node that will cause me a duplicate when will insert the results to DB again... Please I want to know why that happened.

The flow is :slight_smile:

The code in the second function (checking ) is :slight_smile:

var i;
var in_mac;
let person;
let newmsg;

msg.payload.forEach(item => {
    if (item.in_mac === "f123"){
        person = "tom";
    } else if (item.in_mac === "abc"){
        person = "james";
   newmsg = {employee: person, Date:new Date().toLocaleDateString(), Time: new Date().toLocaleTimeString()};

    node.send({payload: newmsg});

The results is

a picture.

What are you injecting and what is returned from the database node.

1 Like

Here is it

[{"id":"f9eb90b8.8007a","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"fd47fc9b.c64a2","type":"inject","z":"f9eb90b8.8007a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":626.0142211914062,"y":196.00567626953125,"wires":[["f732f3e7.46086"]]},{"id":"f732f3e7.46086","type":"function","z":"f9eb90b8.8007a","name":"Read","func":"msg.topic  =\"SELECT * from Check_IN\"\nreturn msg;","outputs":1,"noerr":0,"x":758.0000152587891,"y":193,"wires":[["226f5e51.1238f2"]]},{"id":"226f5e51.1238f2","type":"mysql","z":"f9eb90b8.8007a","mydb":"f9b17512.31f4e8","name":"","x":878.0000152587891,"y":193,"wires":[["55eee102.105d4"]]},{"id":"55eee102.105d4","type":"function","z":"f9eb90b8.8007a","name":"Checking","func":"\nvar i;\nvar in_mac;\nlet person;\nlet newmsg;\n\nmsg.payload.forEach(item => {\n    \n    if (item.in_mac === \"f123\"){\n        person = \"tom\";\n        \n    } else if (item.in_mac === \"abc\"){\n        person = \"james\";\n    }\n   newmsg = {employee: person, Date:new Date().toLocaleDateString(), Time: new Date().toLocaleTimeString()};\n\n    node.send({payload: newmsg});\n});\nnode.done();\nreturn;","outputs":1,"noerr":0,"x":1008.0000152587891,"y":193,"wires":[["a54b8ba5.eed2b8"]]},{"id":"a54b8ba5.eed2b8","type":"debug","z":"f9eb90b8.8007a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1178.000015258789,"y":193,"wires":[]},{"id":"f9b17512.31f4e8","type":"MySQLdatabase","z":"","name":"db","host":"","port":"3306","db":"db15400686","tz":""}]

So your problem is that you are doing 2 if's, but what if both if's don't match ? the previous values will remain and thus you see duplicates.

Why don't you just query the database for the mac directly ?

The mac that we read it from db is collected during the online phase while the mac that in the function node collected in offline in order to make a comparison.....

I don't understand.

But you do not have a table with names and macs that you can validate against ?

I have a table stores the mac address and in the function node the name will add after checking ....

Put a debug node (set to display the 'Complete msg object') on the output of the mysql node. Then you will see what is going into the function node - i.e. how many rows are returned...

That is the issue.


Thank, I understand it now

thanks , I did it and I figured out my mistake

It would be helpful for others reading this in the future to explain what the mistake was so they could hopefully gain from your experience.


The problem was as @bakman2 said.

but you haven't shown how you fixed the function node....

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