for-Loop output questions

Hi community,

can you please help me with some issues regarding the loop-output? I want to send customized birthday wishes via contribute "dynamic eMail" once a day. Data are drawn from a spreadsheet as an array.
I want to run through the array, comparing each msg.payload[i].Geburtsdatum with actual date. (The strings to compare follow exactly the same format.)

I think I should put the "return msg" (or node.send?) within the loop to create an input for email-node with every new match. What would the expression be like?
Do I need to output an array containing the flow-vars or something like a "ping" for the email-node because the flows-vars are dynamically set with every loop?

Does the loop stop after mismatch and do I need an "else" with "continue" to go ahead through the list?
Thanks for your support.

grafik

const Liste = [];
var Anrede;
var Nachname;
var Mail;

for (let i = 0; i <= Liste.length; i++) 
{  
    if (flow.get('datum_heute') === msg.payload[i].Geburtsdatum)
    
    {  
        flow.set('Mail', msg.payload[i].Mail)
        flow.set('Anrede', msg.payload[i].Anrede)
        flow.set('Nachname', msg.payload[i].Nachname)
    }    

}
return msg;

Hi and welcome to the forum

You seem to be over using the function nodes and context variable. The flow you have shown in the image seems over complicated. You should try to pass data in the message object. Try to avoid context unless it is absolutely needed.

Node-red is a low code plateform, and what you require to do can be done with a split and switch node.
e.g

[{"id":"5659df32a49fb39c","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"date","v":"2025/01/12","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Geburtsdatum\":\"2025/01/11\",\"Mail\":\"abc@cde.com\",\"Anrede\":\"Alles Gute zum Geburtstag, mögen Sie Reich und Reich regieren\",\"Vorname\":\"Ryker\"},{\"Geburtsdatum\":\"2025/01/12\",\"Mail\":\"abc@cde.com\",\"Anrede\":\"Alles Gute zum Geburtstag, möge Ihr Leben mit Glück und Erfolg gesegnet sein\",\"Vorname\":\"Felix\"}]","payloadType":"json","x":432.1991271972656,"y":1172.6343994140625,"wires":[["530d65e03f140596"]]},{"id":"530d65e03f140596","type":"split","z":"667cec54c048503c","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":590,"y":1180,"wires":[["8ac201f1ab539450"]]},{"id":"8ac201f1ab539450","type":"switch","z":"667cec54c048503c","name":"","property":"payload.Geburtsdatum","propertyType":"msg","rules":[{"t":"eq","v":"date","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":710,"y":1180,"wires":[["984cf197480bc4af"]]},{"id":"984cf197480bc4af","type":"debug","z":"667cec54c048503c","name":"debug 2579","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":1180,"wires":[]}]

How to import/export a flow

1 Like

Not exactly sure why some of this is present but a simple version

const Liste = (Array.isArray(msg.payload)) ? msg.payload : []    // Validate msg.payload as an Array
const currentDate = flow.get('datum_heute')     // Why is this in a flow variable?

for (let i = 0; i < Liste.length; i++)  {    // Note i < Liste.length
    if (currentDate === Liste[i].Geburtsdatum) {
       msg.payload = {...Liste[i]}

       node.send(msg)
    }
    
}

return

Just seen E1cid's none Function example

Thanks guys, worked pretty well. Appreciate!