Access the object

Hello!
I am getting array of the objects at the output. I am able to access elements of zero but unable to access member of the object. I am trying to get minvalue and maxvalue from that object.

Please help me with this!

msg.payload[0].Agg1_Total_Weight.maxvalue

Sorry But could you help me bit more? I wrote as below. It's not giving me any value.

let one =msg.payload[0].Agg1_Total_Weight.maxvalue;
msg.payload = one;
return msg;

Please paste a flow that demonstrates the issue, for example an inject node that produces a sample message, your function node, and a debug node that shows the wrong/missing data. (You can easily copy a whole message object from a debug node in the debug page.)

Edit: it's hard to see, but you have no msg.payload. The entire message object is an array (bad practice). So it would be msg[0].Agg1_Total_Weight.maxvalue, but you should really fix the producer of the msg.

1 Like

The message does not live in msg.payload. Where does the message come from.
Make sure to go through the documentation to access elements in an object/array.

[{"id":"2dbaa60.7a9125a","type":"change","z":"24076265.de751e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":320,"wires":[["7f9287a9.8549b8"]]},{"id":"a42cb46c.27e798","type":"debug","z":"24076265.de751e","name":"","active":true,"console":"false","xaxis":"_time","complete":"false","x":690,"y":220,"wires":[]},{"id":"7f9287a9.8549b8","type":"function","z":"24076265.de751e","name":"","func":"const date = new Date();\n\nlet day = date.getDate();\nlet dayone = 1;\n//let yesterday = date.getDate() -1 ;\nlet month = date.getMonth() + 1;\nlet year = date.getFullYear();\n\nlet yesterDate = `${year}-${month}-${day} 00:00:01`;\nlet currentDate = `${year}-${month}-${day} 23:00:59`;\n//let yesterDate = `2022-11-01 00:00:01`;\n//let currentDate = `2022-11-01 23:00:59`;\nmsg.from=new Date(yesterDate).toISOString();\nmsg.to=new Date(currentDate).toISOString();\nreturn msg;\n\n\n\n\n\n","outputs":"1","language":"javascript","noerr":0,"x":420,"y":320,"wires":[["3a43ab23.2db5f4"]],"outputLabels":["From"]},{"id":"3a43ab23.2db5f4","type":"read aggregates","z":"24076265.de751e","name":"Agg1_Total_Weight","topic":"ded1a0bfec43400f970e566f8263c6d3/BatchMix/Agg1_Total_Weight","topicData":"{\"asset\":\"ded1a0bfec43400f970e566f8263c6d3\",\"assetName\":\"BatchMix\",\"aspect\":\"403f8515fd754ad298be50f5146fd9a8\",\"aspectName\":\"BatchMix\",\"variables\":[{\"name\":\"Agg1_Total_Weight\",\"variableId\":\"TJ96ba993b8c6b\"}]}","topicLabel":"BatchMix/BatchMix/Agg1_Total_Weight","assetName":"","aspectName":"","period":"60","offset":"0","mode":"interval","from":"","datetimepickerFrom":"","to":"","datetimepickerTo":"","timezoneoffset":0,"intervalValue":0,"intervalUnit":"month","count":0,"api":"aggregates","x":610,"y":320,"wires":[["a42cb46c.27e798","640c6afa.da8be4"]]},{"id":"fbd7743e.a46f38","type":"ui_button","z":"24076265.de751e","name":"","group":"6b69c47b.954aec","order":0,"width":0,"singleBrowser":false,"height":0,"passthru":false,"label":"One Month","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","powerMode":false,"x":70,"y":320,"wires":[["2dbaa60.7a9125a"]]},{"id":"a11ecc24.6c475","type":"debug","z":"24076265.de751e","name":"","active":false,"console":"false","xaxis":"_time","complete":"false","x":1050,"y":320,"wires":[]},{"id":"640c6afa.da8be4","type":"function","z":"24076265.de751e","name":"","func":"\nlet one =msg.payload[0].Agg1_Total_Weight.maxvalue;\n\n//msg.payload = one;\nreturn msg;\n\n\n","outputs":1,"language":"javascript","noerr":0,"x":820,"y":320,"wires":[["a11ecc24.6c475"]],"_type":"node"},{"id":"6b69c47b.954aec","type":"ui_group","z":"","name":"OneMonth","tab":"7ecbab01.42a594","order":1,"disp":true,"width":"20","collapse":false,"_type":"config"},{"id":"7ecbab01.42a594","type":"ui_tab","z":"","name":"DATA","icon":"dashboard","publicAccess":"true","fmAccess":"none","fmAccessType":"private","assetType":"","asset":"","assetName":"","assetText":"","image":"","description":"","hideTitleBar":false,"_type":"config"}]

Aggregate node is provided by the mindsphere. Which is giving me output as below in debug page

[{"Agg1_Total_Weight":{"firsttime":"2022-12-01T03:57:49.743Z","average":3179369.029846638,"countuncertain":0,"maxvalue":3973255.2,"firstvalue":2368975.8,"countbad":928,"mintime":"2022-12-01T03:57:49.743Z","lastvalue":3973255.2,"sum":48510812657.4,"minvalue":2368975.8,"maxtime":"2022-12-27T12:30:38.374Z","sd":472470.00319455424,"lasttime":"2022-12-27T12:31:38.380Z","countgood":15258},"starttime":"2022-11-30T23:00:00Z","endtime":"2022-12-31T23:00:00Z"}]
Screenshot (94)

Did you see my edit above?

Oh, sorry I just read it and did the same but It's giving me error as below
Error: Cannot read properties of undefined (reading 'Agg1_Total_Weight')

You pasted your flow above, but it has the read aggregate node, which I don't have. You need to copy the message going into your function node and paste that here... Ideally with your function node code as well.

Read aggregate node's output going into function node is as below ...

[{"Agg1_Total_Weight":{"firsttime":"2022-12-01T03:57:49.743Z","average":3180098.7095534313,"countuncertain":0,"maxvalue":3976252.5,"firstvalue":2368975.8,"countbad":928,"mintime":"2022-12-01T03:57:49.743Z","lastvalue":3976252.5,"sum":48566467492.3,"minvalue":2368975.8,"maxtime":"2022-12-28T03:57:19.256Z","sd":472867.36424514063,"lasttime":"2022-12-28T03:57:19.256Z","countgood":15272},"starttime":"2022-11-30T23:00:00Z","endtime":"2022-12-31T23:00:00Z"}]

Function node:
What I have written before

let one= msg.payload[0];
msg.payload= one.maxvalue;
return msg;

Then edited

let one =msg.payload[0].Agg1_Total_Weight.maxvalue;
msg.payload = one;
return msg;

anyone of two is not giving any value.

then

let one =msg[0].Agg1_Total_Weight.maxvalue;
msg.payload = one;
return msg;

which is giving error...
Error: Cannot read properties of undefined (reading 'Agg1_Total_Weight')

Haha, this is actually difficult to reproduce 'cause Node-RED is doing everything it can to prevent sending a msg that is an array... That read-aggregate node is pretty messed-up...

Try this: In the function node, add as first line:

node.warn(msg)

and then screen shot or paste what you get in the debug pane.

The other thing I have tried is as below.
Output of the read aggregate node in function node and in function node written as below..

let one =msg.payload[0];
msg.payload = one;
return msg;

Then output of the function node in split node.Which is giving me 3 parts as below
1:
{"firsttime":"2022-12-01T03:57:49.743Z","average":3180098.7095534313,"countuncertain":0,"maxvalue":3976252.5,"firstvalue":2368975.8,"countbad":928,"mintime":"2022-12-01T03:57:49.743Z","lastvalue":3976252.5,"sum":48566467492.3,"minvalue":2368975.8,"maxtime":"2022-12-28T03:57:19.256Z","sd":472867.36424514063,"lasttime":"2022-12-28T03:57:19.256Z","countgood":15272}

2:
2022-11-30T23:00:00Z

3:
2022-12-31T23:00:00Z

Output of this split node in change node with
Set msg.payload to JSONata (parts.index+1) & ": " & payload
which is giving me above 3 result as three as string.
Ss, unable to fing what should be done after split node.

[{"id":"f21a5000.6961e","type":"function","z":"24076265.de751e","name":"","func":"//let array_length = msg.payload.length;\nlet one =msg.payload[0];\n//let last= msg.payload[array_length - 1];\n//msg.payload =  one;\nmsg.payload = one;\nreturn msg;\n\n\n/*\nconst Agg1_Total_Weight=msg.payload.maxvalue;\n//let one= Agg1_Total_Weight.maxvalue;\n//msg.payload=Agg1_Total_Weight;\nmsg.payload=Agg1_Total_Weight;\nreturn msg;\n*/","outputs":1,"language":"javascript","noerr":0,"x":820,"y":320,"wires":[["10bb887e.7a6478"]],"_type":"node"},{"id":"10bb887e.7a6478","type":"split","z":"24076265.de751e","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":990,"y":260,"wires":[["3ed5babb.352146","bf79525b.fae62","8fa7f64.6e6be08"]],"_type":"node"},{"id":"3ed5babb.352146","type":"change","z":"24076265.de751e","name":"Prepend line number","rules":[{"t":"set","p":"payload","pt":"msg","to":"(parts.index+1) & \": \" & payload","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":160,"wires":[["6018fc8.b439e04"]],"_type":"node"},{"id":"6018fc8.b439e04","type":"debug","z":"24076265.de751e","name":"","active":true,"console":"false","xaxis":"_time","complete":"false","x":1310,"y":160,"wires":[]},{"id":"bf79525b.fae62","type":"debug","z":"24076265.de751e","name":"","active":true,"console":"false","xaxis":"_time","complete":"false","x":1190,"y":340,"wires":[]}]

copy from debug pane

Screenshot (99)
{"payload":[{"Agg1_Total_Weight":{"firsttime":"2022-12-01T03:57:49.743Z","average":3180098.7095534313,"countuncertain":0,"maxvalue":3976252.5,"firstvalue":2368975.8,"countbad":928,"mintime":"2022-12-01T03:57:49.743Z","lastvalue":3976252.5,"sum":48566467492.3,"minvalue":2368975.8,"maxtime":"2022-12-28T03:57:19.256Z","sd":472867.36424514063,"lasttime":"2022-12-28T03:57:19.256Z","countgood":15272},"starttime":"2022-11-30T23:00:00Z","endtime":"2022-12-31T23:00:00Z"}],"socketid":"LQ0JrVMiN7Iwvf_gABbF","_dashId":"fbd7743e.a46f38","_msgid":"4d69e1f6.35116","__history":["2dbaa60.7a9125a","7f9287a9.8549b8","3a43ab23.2db5f4","640c6afa.da8be4"],"_starttime":1672208743664,"_correlationId":"75119f00867811ed8156b1e347cd4d61","from":"2022-12-28T00:00:01.000Z","to":"2022-12-28T23:00:59.000Z","topic":"ded1a0bfec43400f970e566f8263c6d3/BatchMix/Agg1_Total_Weight"}

In this case you have a payload... It seems you're trying so many combinations so fast that you're getting wires crossed...
Just put a function node after the read aggregate value and put the node.warn from above in the very first line, and then paste a screen shot of the result in the debug pane here...

Did the same

{"payload":[{"Agg1_Total_Weight":{"firsttime":"2022-07-09T09:23:39.568Z","average":1396018.541708383,"countuncertain":0,"maxvalue":2363601.8,"firstvalue":499117.66,"countbad":679,"mintime":"2022-07-09T09:23:39.568Z","lastvalue":2363601.8,"sum":44396181663.41,"minvalue":499117.66,"maxtime":"2022-07-30T20:22:34.868Z","sd":548327.8553248801,"lasttime":"2022-07-31T21:59:44.158Z","countgood":31802},"starttime":"2022-06-30T22:00:00Z","endtime":"2022-07-31T22:00:00Z"},{"Agg1_Total_Weight":{"firsttime":"2022-07-31T22:00:15.537Z","average":2469048.5449149716,"countuncertain":0,"maxvalue":2563734,"firstvalue":2363601.8,"countbad":5,"mintime":"2022-07-31T22:00:15.537Z","lastvalue":2563734,"sum":7404676586.2,"minvalue":2363601.8,"maxtime":"2022-08-02T05:57:42.179Z","sd":86300.84133273154,"lasttime":"2022-08-03T12:32:36.633Z","countgood":2999},"starttime":"2022-07-31T22:00:00Z","endtime":"2022-08-31T22:00:00Z"},{"Agg1_Total_Weight":{"firsttime":"2022-09-28T05:36:14.117Z","average":344984.37455096253,"countuncertain":0,"maxvalue":544302.25,"firstvalue":549.18005,"countbad":42,"mintime":"2022-09-28T05:36:14.117Z","lastvalue":544302.25,"sum":1344059123.25055,"minvalue":549.18005,"maxtime":"2022-09-30T12:46:31.851Z","sd":183724.5805056436,"lasttime":"2022-09-30T21:59:33.802Z","countgood":3896},"starttime":"2022-08-31T22:00:00Z","endtime":"2022-09-30T22:00:00Z"},{"Agg1_Total_Weight":{"firsttime":"2022-09-30T22:00:03.802Z","average":577885.749993223,"countuncertain":0,"maxvalue":873711.75,"firstvalue":544302.25,"countbad":5,"mintime":"2022-09-30T22:00:03.802Z","lastvalue":873711.75,"sum":5968981911.68,"minvalue":544302.25,"maxtime":"2022-10-12T08:29:20.715Z","sd":80521.6777314963,"lasttime":"2022-10-12T08:56:16.278Z","countgood":10329},"starttime":"2022-09-30T22:00:00Z","endtime":"2022-10-31T23:00:00Z"},{"Agg1_Total_Weight":{"firsttime":"2022-11-07T09:30:57.523Z","average":1768738.6248915496,"countuncertain":0,"maxvalue":2367551,"firstvalue":1160052.4,"countbad":0,"mintime":"2022-11-07T09:30:57.523Z","lastvalue":2367551,"sum":20386481390.5,"minvalue":1160052.4,"maxtime":"2022-11-30T12:29:02.908Z","sd":350493.1448188479,"lasttime":"2022-11-30T12:30:02.918Z","countgood":11526},"starttime":"2022-10-31T23:00:00Z","endtime":"2022-11-30T23:00:00Z"},{"Agg1_Total_Weight":{"firsttime":"2022-12-01T03:57:49.743Z","average":3180098.7095534313,"countuncertain":0,"maxvalue":3976252.5,"firstvalue":2368975.8,"countbad":928,"mintime":"2022-12-01T03:57:49.743Z","lastvalue":3976252.5,"sum":48566467492.3,"minvalue":2368975.8,"maxtime":"2022-12-28T03:57:19.256Z","sd":472867.3642451427,"lasttime":"2022-12-28T03:57:19.256Z","countgood":15272},"starttime":"2022-11-30T23:00:00Z","endtime":"2022-12-31T23:00:00Z"}],"socketid":"KIr-alDoWA3CVvNCABbZ","_dashId":"876d2cb4.7d82c","topic":"ded1a0bfec43400f970e566f8263c6d3/BatchMix/Agg1_Total_Weight","_msgid":"463c1955.d75c98","__history":["e6e5ddae.06537","f3889b7e.ac9f78"],"_starttime":1672210381870,"_correlationId":"452eb8f0867c11ed90307b97fee8c65f"}

Hmmmm, when I put node.warn(msg) as first line into a function node I get something looking quite different:

image

Notice how it says "function: (warn)" in the second line in red...

Yes, but somehow, I am not getting message as yours.

hmmm, maybe that requires a setting, I forget. Try node.error(msg) instead.

again It's ...

Excellent, so there's a payload in that msg. Now comment out the first line with the node.error() and put in the second line:

return { payload: msg.payload[0].Agg1_Total_Weight.minvalue }

Then add a debug node behind the function node and you should see 2368975.8 appear...