# Node namespace confusion on my part (?)

**URL:** <https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144>\
**Category:** Developing Nodes\
**Created:** [7 August 2018 22:40 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144 "2018-08-07T22:40:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [7 August 2018 22:40 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/1 "2018-08-07T22:40:17Z")

</div>

I have a working node (node-red-contrib-mytimeout) and I have a user who has 12 of these nodes in 1 tab. I'm investigating why some nodes, sometimes fails to finish counting down (see below),. I've seen this before occur when the ticks (they are what I count down) is set to -1 before reaching 0. While I don't think that can happen in my current code.

What I'd like to know is if the node's might share namespace if the nodes all had the same node? Or is it something else I might not yet understand.

> <https://github.com/linuxha/node-red-contrib-mytimeout/issues/5>

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [8 August 2018 11:10 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/2 "2018-08-08T11:10:56Z")

</div>

I didn't go in depth into you code, so this is only a guess:

Try to change all global vars to this e.g.:

```auto
var timeout = parseInt(n.timer||30); 

```

```auto
this.timeout = parseInt(n.timer||30); 

```

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [8 August 2018 12:10 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/3 "2018-08-08T12:10:24Z")

</div>

Huge thanks, I don't quite understand the scope of Javascript (as my code shows). I'll fix that as I think I have a couple of values like that.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 August 2018 12:58 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/4 "2018-08-08T12:58:07Z")

</div>

> [@cflurin](#):
>
> this.timeout = parseInt(n.timer||30);

@ncherry, as an aside is this really what you want, or do you want  
`this.timeout = parseInt(n.timer)||30;`

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [8 August 2018 18:21 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/5 "2018-08-08T18:21:01Z")

</div>

I forget why that came about ... but I'll correct it to parseInt(n.timer)||30. When I first created this node it was a bit of cargo cult programming. Since then I've let well enough alone. Now is s good time to finish the rest of the cleanup. There is still a lot of cruft left that needs to be removed or corrected.

Thanks

**Update:** I recall now, if n wasn't defined it would get an exception. With parseInt(n.timer||30) I'd get 30 instead of the exception. Probably not a good way of dealing with the exception but as I said, cargo cult programming. I understand nodes a bit better now. Though I'm certain I have a lot more to learn

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 August 2018 20:10 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/6 "2018-08-08T20:10:48Z")

</div>

Perfectly reasonable approach on my book. The parseInt must have something valid to work on.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 August 2018 20:31 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/7 "2018-08-08T20:31:20Z")

</div>

That is fine if you are concerned about n.timer not defined. However, if n.timer was set to "hello" for example then n.timer||30 would be "hello" and parseInt would return NaN. However parseInt("hello")||30 is 30. As I said, it depends what you are trying to protect against. I don't know what the timer is without looking at the code again, but also note that will not be able to set it to 0 with either of those techniques.

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [9 August 2018 04:29 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/8 "2018-08-09T04:29:27Z")

</div>

Oh yeah, learned that one the hard way. Stared at that for several minutes until it hit me that 0 == false (oops).

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [9 August 2018 13:06 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/9 "2018-08-09T13:06:06Z")

</div>

In similar cases I do it this way:

```auto
this.timeout = typeof n !== "undefined" && typeof n.timer !== "undefined" ? parseInt(n.timer) : 30;

```

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [9 August 2018 22:06 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/10 "2018-08-09T22:06:27Z")

</div>

> [@cflurin](#):
>
> this.timeout = typeof n !== "undefined" && typeof n.timer !== "undefined" ? parseInt(n.timer) : 30;

Excellent, works for 0 and a undefined (n or n.timer).

Thanks, I'll make a note of that. I'm sure, I'll run across this again

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 October 2018 12:22 UTC](https://discourse.nodered.org/t/node-namespace-confusion-on-my-part/2144/11 "2018-10-06T12:22:39Z")

</div>

Just a comment on variable scope. If you are using newer NodeJS versions, you should use `let` or `const` instead of `var`. `let` ensures that a variable is locally scoped.
