Problems with foreach function

foreach
I don't know why is appearing this error, some help please.

This is my full function...

const time = new Date(msg.payload);
const hour = time.getHours();
const minute = time.getMinutes();
let timef = "";
if(minute<10){
    if(hour>9){
        timef = String(hour) + ":0" + String(minute);
    } else {
        timef = "0"+ String(hour) + ":0" + String(minute);
    }
} else {
    if(hour>9){
        timef = String(hour) + ":" + String(minute);
    } else {
        timef = "0"+ String(hour) + ":" + String(minute);
    }
}
const msgOut = flow.get("msgOut");
msgOut.forEach(element => {
    if(element.time == timef) node.send({payload:element.command,item:element.item});
});
node.done();
return;

This error is because your flow variable msgOut is not an array.
This could be because it is undefined as it wasn’t set yet or it could be because its some other type than an array.
You can check the content of the flow variable in the context tab of the right sidebar.
If it is because of a race condition where it wasn’t set yet when this function is called you could change the flow.get line to const msgOut = flow.get("msgOut") || []; to initialize it to an empty array in this case.

1 Like

Thanks it works.

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