# Error while executing code with function node

**URL:** https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443
**Category:** General
**Created:** [29 May 2021 11:05 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443 "2021-05-29T11:05:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [29 May 2021 11:05 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/1 "2021-05-29T11:05:05Z")

</div>

I am facing an issue using a third-party library "danfojs-node" with a function node.

I am running this snippet with node.js and it produces the expected result.  
Code:

```javascript
const dfd = require("danfojs-node");
json_data = [
    { A: 0.4612, B: 4.28283, C: -1.509, D: -1.1352 },
    { A: 0.5112, B: -0.22863, C: -3.39059, D: 1.1632 },
    { A: 0.6911, B: -0.82863, C: -1.5059, D: 2.1352 },
    { A: 0.4692, B: -1.28863, C: 4.5059, D: 4.1632 },
];
df = new dfd.DataFrame(json_data);
df.print();

```

Result is:

```auto
╔═══╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ A │ B │ C │ D ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 0 │ 0.4612 │ 4.28283 │ -1.509 │ -1.1352 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 1 │ 0.5112 │ -0.22863 │ -3.39059 │ 1.1632 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 2 │ 0.6911 │ -0.82863 │ -1.5059 │ 2.1352 ║
╟───┼───────────────────┼───────────────────┼───────────────────┼───────────────────╢
║ 3 │ 0.4692 │ -1.28863 │ 4.5059 │ 4.1632 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

```

When I run the same code with the node-red function node, it gives me an error. (See the attached files)  
"Error: File format not supported"

Node-RED version: 1.3.3  
Node.js version: v12.16.1  
The latest version of danfojs-node (0.2.6) has been used in both cases.

 ![Screenshot from 2021-05-27 15-28-49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/750af6ef23e5b868010e36cd040246b3c2bb166e.png)  
 ![Screenshot from 2021-05-27 15-29-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7823dd89a763d468e0bb1055e7621ef0f86a16d.png)

---

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [29 May 2021 11:05 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/2 "2021-05-29T11:05:52Z")

</div>

![Screenshot from 2021-05-27 15-29-17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b19c9f693157f59fc46036180e57fca5be83d830.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [29 May 2021 11:11 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/3 "2021-05-29T11:11:17Z")

</div>

Is this not the same issue as this? : [Wrong exception thrown for empty dataframe · Issue #37 · opensource9ja/danfojs · GitHub](https://github.com/opensource9ja/danfojs/issues/37)

---

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [29 May 2021 11:14 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/4 "2021-05-29T11:14:01Z")

</div>

I further inspected the source code of "danfojs-node" and found that

is object check is passing when I am running the above code snippet with node.js and it fails when I execute the code with node-red function.

Source code:  
The code where its checking for isObject is here:

> <https://github.com/opensource9ja/danfojs/blob/c8b0f35057d8b42da7c5a7203311d97b2e9e914c/danfojs-node/dist/core/utils.js#L40>

return value && typeof value === "object" && value.constructor === Object;

The above util function gets called when we create new dataframe (df = new dfd.DataFrame(json\_data); )

> <https://github.com/opensource9ja/danfojs/blob/c8b0f35057d8b42da7c5a7203311d97b2e9e914c/danfojs-node/dist/core/generic.js#L36>

To be specific This check is failing when executing code with node-red function  
value.constructor === Object

If you have any ideas why its behaving this way, please let me know.  
Thanks

---

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [29 May 2021 11:16 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/5 "2021-05-29T11:16:55Z")

</div>

> [@Steve-Mcl](#):
>
> Is this not the same issue as this? : [Wrong exception thrown for empty dataframe · Issue #37 · opensource9ja/danfojs · GitHub](https://github.com/opensource9ja/danfojs/issues/37)

I believe it's not the same. The code snippet (i attached above) is working fine when directly executing with the node.js. it's only throwing the error when code is being executed by node-red function.

---

<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: [29 May 2021 11:59 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/6 "2021-05-29T11:59:05Z")

</div>

This is likely due to the way the function node runs it's code inside the VM modules - that can have odd side effects in code that tries to do certain types of comparison between an object created inside the VM and one created outside.

If that is the case, there isn't a short term solution I can give you - it's a consequence of how the node works.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [29 May 2021 12:30 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/7 "2021-05-29T12:30:33Z")

</div>

I was heading to the same conclusion & now you say it, I believe that is the issue.

@vinaysudani I tried it in nodejs and a function node and see the same issue you do.

As a workaround, I changed the `__is_object` function to ` return value && typeof value === "object" && value.constructor && value.constructor.name === "Object"` & it works in the function node vm.

Maybe you could raise an issue against the repo stating it doesn't work as expected in a nodejs vm?

---

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [29 May 2021 13:05 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/8 "2021-05-29T13:05:08Z")

</div>

Thanks, @knolleary @Steve-Mcl for your quick replies.

I will report an issue on "danfojs-node" to look into this senario.

---

<div class="post-metadata">

### Author: ![vinaysudani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vinaysudani/32/42601_2.png) [@vinaysudani](https://discourse.nodered.org/u/vinaysudani)
#### Post date: [1 June 2021 06:32 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/9 "2021-06-01T06:32:48Z")

</div>

This issue is now resolved by the merge of this pull request: [Fixes #206 Testing if value is an object by risenW · Pull Request #208 · opensource9ja/danfojs · GitHub](https://github.com/opensource9ja/danfojs/pull/208)

Thanks, everyone for your participation in resolving the issue.

---

<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: [31 July 2021 06:33 UTC](https://discourse.nodered.org/t/error-while-executing-code-with-function-node/46443/10 "2021-07-31T06:33:09Z")

</div>

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