# Payload and JS plain object

**URL:** <https://discourse.nodered.org/t/payload-and-js-plain-object/12100>\
**Category:** General\
**Created:** [12 June 2019 09:34 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100 "2019-06-12T09:34:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JbIPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jbips/32/9246_2.png) [@JbIPS](https://discourse.nodered.org/u/JbIPS)\
**Post date:** [12 June 2019 09:34 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/1 "2019-06-12T09:34:39Z")

</div>

Hi,

I'm creating a flow to store form data into a Firestore DB with [node-red-contrib-cloud-firestore](https://flows.nodered.org/node/node-red-contrib-cloud-firestore).

On input, the payload contains a object with nested object but when Firestore check it, it fails the test on `Object.getPrototypeOf(input) === Object.prototype`.

Is something done to the payload when sending it to nodes that could change such a behaviour?

Thank you for your help

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [12 June 2019 09:47 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/2 "2019-06-12T09:47:10Z")

</div>

Are you using a Function node to create the object you pass to the firestore node? If so, it will likely fail that test due to the internal workings of the Function node. I won't bore you with the details unless you really want to know.

One workaround is to create the object in a Change node instead. If you really need the Function node, then create the object in a Change node and then pass that to the Function node to populate its values. I _think_ that would work.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [12 June 2019 09:48 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/3 "2019-06-12T09:48:17Z")

</div>

Another workaround would be to pass the output of your Function node through two JSON nodes - one to encode it to a string and then one to parse it back to an Object. That new object should pass the test being done by the firestore node.

---

<div class="post-metadata">

**Author:** ![JbIPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jbips/32/9246_2.png) [@JbIPS](https://discourse.nodered.org/u/JbIPS)\
**Post date:** [12 June 2019 09:52 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/4 "2019-06-12T09:52:05Z")

</div>

Are you saying an object created in a Function (you're right, that's my case) won't be a plain object? If it's not too much to ask I'm interested in the details 🙂

For the record, here's my Function with the incriminated object:

```auto
const allowedKeys = ['firstname', 'name', 'email'];
const contact = {name: null, company: null, mail: null};
msg.payload = msg.payload.form_response.answers.reduce((memo, ans) => {
    const [key, subkey] = ans.field.ref.split('_');
    const value = ans[ans.type];
    switch(true) {
        case key === 'contact':
            contact[subkey] = value;
            break;
        case key === 'firstname':
            memo.push({[key]: value, contact});
            break;
        case allowedKeys.includes(key): 
            memo[memo.length - 1][key] = value;
            break;
        default: console.debug(`${key} key not allowed`);
    }
    return memo;
}, []);
return msg;

```

If a Change would set `msg.contact` to a new empty object and pass it to the Function, would that work?

Thanks a lot!

EDIT: The Change workaround works! That's good to have but I'm still interested in the why 😉

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [12 June 2019 10:19 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/5 "2019-06-12T10:19:44Z")

</div>

> [@JbIPS](#):
>
> That's good to have but I'm still interested in the why

The Function node uses the node vm module to run its code in a sandbox. That sandbox gets its own instances of all the primitive types, like Object.

The strict equality test used by the firestore node is checking whether the prototype of your object is exactly the same object in memory as the prototype of Object.

As the Function node has its own copy of Object, then whilst they are identical things, they are not exactly the same instance in memory so the test fails.

---

<div class="post-metadata">

**Author:** ![JbIPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jbips/32/9246_2.png) [@JbIPS](https://discourse.nodered.org/u/JbIPS)\
**Post date:** [12 June 2019 13:47 UTC](https://discourse.nodered.org/t/payload-and-js-plain-object/12100/6 "2019-06-12T13:47:56Z")

</div>

Thank you, that's much clearer now! I'll keep that in mind for my future development !
