Msg assignment syntax error

I'm accessing openweather.org and pulling down a matrix like so.
array[40]
[0 … 9]
[10 … 19]
[20 … 29]
20: object
21: object
22: object
dt: 1587319200
main: object
weather: array[1]
clouds: object
wind: object
speed: 2.55
deg: 232
rain: object
3h: 0.49
sys: object
dt_txt: "2020-04-19 18:00:00"
23: object
24: object

The code to access works with the wind array but not the rain object.
Here's my code:

Blockquote
var rainAmt0=msg.payload[0].rain.3h); //ng
var windSpeed0=msg.payload[0].wind.speed; //OK
Blockquote

There is an issue with the '3' which causes a syntax error. "unexpected '.' ".
So what am I doing wrong?

Yes and no :slight_smile: one of the joys of JavaScript I'm afraid. You need to use the alternate syntax for accessing properties that start with numbers (or contain spaces or other non standard characters ). So you need rain["3h"]

Still throwing error:
"TypeError: Cannot read property '3h' of undefined"

Blockquote
var rainAmt0=msg.payload[0].rain["3h"];
var rainAmtOut={payload:rainAmt0};
Blockquote

Thanks.

msg.payload[0].rain << must be undefined cos it cant get >> ["3h"]

try putting a debug just before where this function is & see what is in msg.payload before it reaches this function - my bet is its not what you expect.

The documentation on "working with messages" has some excellent tips on this (well worth 10mins of your time)

EDIT...
also, looking at your output above, it doesnt look like 3h is a member property of rain
Its a good idea to use the copy path button in the debug side-bar to avoid errors like this (it in the "working with messages" documentation)

Blockquote
wind: object
speed: 2.55
deg: 232
rain: object
3h: 0.49
Blockquote
This is part of an array with 40+ elements.

image
PS, you are viewing element [12] not element [0]

have you checked element [0].rain has a 3h property?

The 'path' string is yet another tool added to my arsenal.
Using copy in debug statement the path is: var rainAmt0=msg.payload[4].rain["3h"];
No syntax...Thank you.
It seems that payload[0] sometimes has rain object and sometimes not.
Problem solved.
Thanks Steve-Mcl and dceejay.

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