# Problems with foreach function

**URL:** <https://discourse.nodered.org/t/problems-with-foreach-function/30166>\
**Category:** General\
**Created:** [16 July 2020 04:37 UTC](https://discourse.nodered.org/t/problems-with-foreach-function/30166 "2020-07-16T04:37:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [16 July 2020 04:37 UTC](https://discourse.nodered.org/t/problems-with-foreach-function/30166/1 "2020-07-16T04:37:22Z")

</div>

![foreach](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/890fe1eafc56272bd65716d29f92db7d055ef6e0.png)  
I don't know why is appearing this error, some help please.

This is my full function...

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [16 July 2020 06:03 UTC](https://discourse.nodered.org/t/problems-with-foreach-function/30166/2 "2020-07-16T06:03:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [16 July 2020 15:40 UTC](https://discourse.nodered.org/t/problems-with-foreach-function/30166/3 "2020-07-16T15:40:09Z")

</div>

Thanks it works.

---

<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:** [30 July 2020 15:40 UTC](https://discourse.nodered.org/t/problems-with-foreach-function/30166/4 "2020-07-30T15:40:10Z")

</div>

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