# Can not get output from function

**URL:** <https://discourse.nodered.org/t/can-not-get-output-from-function/52553>\
**Category:** General\
**Created:** [22 October 2021 08:33 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553 "2021-10-22T08:33:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiredfruit](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Tiredfruit](https://discourse.nodered.org/u/Tiredfruit)\
**Post date:** [22 October 2021 08:33 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/1 "2021-10-22T08:33:46Z")

</div>

I would like to handle this json file, extract a number from three arrays and make them a new array. The expectation is:  
[[1,2,3],[4,5,6],[7,8,9]........ ]  
I have changed my code and tested in single javascript tester and it works, but there is no output in node-red. I think the problem is, the output of the function must be message but mine is not. I don't know if it's right, so I come here for some help...

```auto
var angle_value = { payload: msg.payload["tightening steps"]["2"].graph["angle values"] };
var torque_value = { payload: msg.payload["tightening steps"]["2"].graph["torque values"] };
var time_value = { payload: msg.payload["tightening steps"]["2"].graph["time values"] };
var data = [];
for (i=0;i<angle_value.length;i++){
    var b = [];
    b[0] = angle_value[i];
    b[1] = torque_value[i];
    b[2] = time_value[i];
    data[i] = b;
}
return data;

```

Flow below

```auto
[
    {
        "id": "4eed35556f5d6390",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "b9eb5c085c98f922",
        "type": "file in",
        "z": "4eed35556f5d6390",
        "name": "curve_sample",
        "filename": "C:\\Users\\IHS4SZH\\Desktop\\curve_sample.txt",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 120,
        "y": 160,
        "wires": [
            [
                "97bc182de7dec1f2"
            ]
        ]
    },
    {
        "id": "97bc182de7dec1f2",
        "type": "json",
        "z": "4eed35556f5d6390",
        "name": "",
        "property": "payload",
        "action": "obj",
        "pretty": false,
        "x": 110,
        "y": 240,
        "wires": [
            [
                "d7e6efd5cd08d163"
            ]
        ]
    },
    {
        "id": "2de8bfab32f6a91e",
        "type": "inject",
        "z": "4eed35556f5d6390",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 80,
        "wires": [
            [
                "b9eb5c085c98f922"
            ]
        ]
    },
    {
        "id": "547da926858ed6cf",
        "type": "debug",
        "z": "4eed35556f5d6390",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 110,
        "y": 400,
        "wires": []
    },
    {
        "id": "d7e6efd5cd08d163",
        "type": "function",
        "z": "4eed35556f5d6390",
        "name": "",
        "func": "var angle_value = { payload: msg.payload[\"tightening steps\"][\"2\"].graph[\"angle values\"] };\nvar torque_value = { payload: msg.payload[\"tightening steps\"][\"2\"].graph[\"torque values\"] };\nvar time_value = { payload: msg.payload[\"tightening steps\"][\"2\"].graph[\"time values\"] };\nvar data = [];\nfor (i=0;i<angle_value.length;i++){\n var b = [];\n b[0] = angle_value[i];\n b[1] = torque_value[i];\n b[2] = time_value[i];\n data[i] = b;\n}\nreturn data;\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 120,
        "y": 320,
        "wires": [
            [
                "547da926858ed6cf"
            ]
        ]
    }
]

```

---

<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:** [22 October 2021 08:35 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/2 "2021-10-22T08:35:19Z")

</div>

`data` is not a valid message format. That's why it isn't working.

---

<div class="post-metadata">

**Author:** ![Tiredfruit](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Tiredfruit](https://discourse.nodered.org/u/Tiredfruit)\
**Post date:** [22 October 2021 08:36 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/3 "2021-10-22T08:36:19Z")

</div>

Yes, I agree. But I wonder if there is some way to fix it..

---

<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:** [22 October 2021 08:37 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/4 "2021-10-22T08:37:59Z")

</div>

Yes, of course, put the data into an appropriate format. The docs have all the information on msg formats.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 October 2021 08:51 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/5 "2021-10-22T08:51:10Z")

</div>

The returned message needs to be an object, you can add your data to a property of that object.  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

```auto
msg.data = data;
return msg;

```

This will return the original object, plus the new data property. You can set your data to any property you wish. you could clear all original data by first setting `msg = {};`

---

<div class="post-metadata">

**Author:** ![Tiredfruit](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Tiredfruit](https://discourse.nodered.org/u/Tiredfruit)\
**Post date:** [22 October 2021 09:03 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/6 "2021-10-22T09:03:33Z")

</div>

Thanks a lot! I have tried the code before:

```auto
msg.data = data;
return msg;

```

But unfortunately it didn't work.I have also read the doc you mentioned hours ago. Facts have proved that I may not read it seriously enough.  
I saw a similar problem in another post, and someone mentioned your solution. After executing these two lines of code, the returned result is the content of the original file. If `msg = {};` is executed, the returned content is "undefined".  
I wonder if the problem lies in the creation of `data`, or the data type in the loop?

---

<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:** [22 October 2021 09:17 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/7 "2021-10-22T09:17:06Z")

</div>

> [@Tiredfruit](#):
>
> unfortunately it didn't work

In what way did it not work? We are not telepathic. Did you feed it into a debug node set to Output Complete Message in order to check what is in msg.data?

If you want the output in msg.payload (which is the usual property for passing on data) then you probably want `msg.payload = data`

---

<div class="post-metadata">

**Author:** ![Tiredfruit](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Tiredfruit](https://discourse.nodered.org/u/Tiredfruit)\
**Post date:** [22 October 2021 09:25 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/8 "2021-10-22T09:25:53Z")

</div>

Thank you Colin, I have checked the complete message.

The problem is, the `data` is empty. I think maybe something wrong with the loop. I will check it and try to figure it out.

---

<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:** [22 October 2021 09:30 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/9 "2021-10-22T09:30:05Z")

</div>

If you look in the docs page Writing Functions it shows you how you can use `node.warn()` to output messages to the debug log from inside a function, so you can more easily work out what is going on. For example, after the first line you could add

```auto
node.warn(`angle_value length: ${angle_value.length}`)

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 October 2021 12:02 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/10 "2021-10-22T12:02:37Z")

</div>

> [@Tiredfruit](#):
>
> I wonder if the problem lies in the creation of `data` , or the data type in the loop?

May be you cound show us the original incoming data?  
`var angle_value = { payload: msg.payload["tightening steps"]["2"].graph["angle values"] };`  
looks wrong to as you reference later `angle_value[i]`, this is wrong as you set angle\_value to an object called `payload`. So it should be `angle_value.payload[i]`.

---

<div class="post-metadata">

**Author:** ![Tiredfruit](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@Tiredfruit](https://discourse.nodered.org/u/Tiredfruit)\
**Post date:** [22 October 2021 12:28 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/11 "2021-10-22T12:28:00Z")

</div>

Thank you, this is the problem.

There are 2 problems found:  
1.`data` is under wrong type, `msg.payload = data` fix it. This makes `payload` in message become `data`. Alternatively, you can use `msg.data = data;`, to add a property `data` to the message.

2.It's not angle\_value, but angle\_value.payload.

Thank you for helping me solve this problem. I have learned a lot.

---

<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:** [5 November 2021 12:28 UTC](https://discourse.nodered.org/t/can-not-get-output-from-function/52553/12 "2021-11-05T12:28:10Z")

</div>

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