Can not get output from function

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...

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

[
    {
        "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"
            ]
        ]
    }
]

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

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

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

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

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 = {};

Thanks a lot! I have tried the code before:

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?

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

1 Like

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.

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

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

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].

1 Like

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.

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