# Nested types get messed up when passed to more than one node

**URL:** <https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006>\
**Category:** General\
**Created:** [10 May 2019 16:46 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006 "2019-05-10T16:46:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [10 May 2019 16:46 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/1 "2019-05-10T16:46:45Z")

</div>

I came across some weired behavior: consider the following function node:

```auto
class MyArray extends Array{}
class MyThing {
    constructor(){
        this.arr = new MyArray()
    }
}
var thing = new MyThing()
return {payload: thing};

```

if you connect a debug node to this which prints out `msg.payload.arr.constructor.name` it will print correctly "MyArray".

But if you connect a second debug node with exactly the same setting, it will print "Array"

 ![image](https://user-images.githubusercontent.com/10807348/57542989-683fc400-7353-11e9-87e6-82b3df7e7874.png)

here is an MCV flow showing the problem:

```auto
[{"id":"3f9bab72.f45d44","type":"function","z":"32bbe58d.19148a","name":"","func":"class MyArray extends Array{}\nclass MyThing {\n constructor(){\n this.arr = new MyArray()\n }\n}\nvar thing = new MyThing()\n\nreturn {payload: thing};","outputs":1,"noerr":0,"x":470,"y":100,"wires":[["d5ae8f14.e3fb3","7abcddf.c434824"]]}]

```

---

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [10 May 2019 16:48 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/2 "2019-05-10T16:48:06Z")

</div>

here is the corresponding bug in my nodes repository [https://github.com/enocean-js/enocean-js/issues/111](https://github.com/enocean-js/enocean-js/issues/111)

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [10 May 2019 17:17 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/3 "2019-05-10T17:17:50Z")

</div>

I wonder if this behaviour is related to the fact that Node-RED clones messages that are send to the additional wires (the first one will get the original msg object).

---

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [10 May 2019 17:54 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/4 "2019-05-10T17:54:04Z")

</div>

investigating further, node-red uses [clone](https://github.com/pvorb/clone) and apparently the bug is caused by that module. there is already a bug report for this particular problem dating back to 2017... it seems the module is not actively maintained anymore ☹

> <https://github.com/pvorb/clone/issues/87>

---

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [10 May 2019 18:08 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/5 "2019-05-10T18:08:23Z")

</div>

digging even deeper, I found this to work:

```auto
class MyArray extends Array{}
class MyThing {
    constructor(){
        this.arr = new MyArray()
    }
}
var thing = new MyThing()

var thing2 = Object.assign(Object.create(thing), thing);

console.log(thing.arr.constructor.name)
console.log(thing2.arr.constructor.name)

```

though i'm not sure i would want to risk breaking stuff replacing `clone` with `Object.assign(Object.create(thing), thing)` ...

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [10 May 2019 18:57 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/6 "2019-05-10T18:57:23Z")

</div>

How about the using spread syntax for cloning your object ?

Something like:

```auto
var thing2 = {...thing};

```

I don´t remember how it differs from object.assign neither how it handles nested properties. I never needed to go on such advanced level in regards to object creation.

---

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [11 May 2019 02:46 UTC](https://discourse.nodered.org/t/nested-types-get-messed-up-when-passed-to-more-than-one-node/11006/7 "2019-05-11T02:46:19Z")

</div>

nice one! the spread operator does work... how could I have missed that 😉  
this would work for my message object, but unfortunately, both ways break the unit tests of NR ☹
