# Msg assignment syntax error

**URL:** https://discourse.nodered.org/t/msg-assignment-syntax-error/24982
**Category:** Dashboard
**Created:** [16 April 2020 22:50 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982 "2020-04-16T22:50:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)
#### Post date: [16 April 2020 22:50 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/1 "2020-04-16T22:50:26Z")

</div>

I'm accessing [openweather.org](http://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?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [16 April 2020 22:54 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/2 "2020-04-16T22:54:52Z")

</div>

Yes and no 🙂 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"]

---

<div class="post-metadata">

### Author: ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)
#### Post date: [16 April 2020 23:33 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/3 "2020-04-16T23:33:09Z")

</div>

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

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

Thanks.

---

<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: [16 April 2020 23:42 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/4 "2020-04-16T23:42:13Z")

</div>

> [@chuckf201](#):
>
> "TypeError: Cannot read property '3h' of undefined"

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)

---

<div class="post-metadata">

### Author: ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)
#### Post date: [17 April 2020 00:12 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/5 "2020-04-17T00:12:13Z")

</div>

> Blockquote  
> wind: object  
> speed: 2.55  
> deg: 232  
> rain: object  
> 3h: 0.49  
> Blockquote  
> This is part of an array with 40+ elements.
> 
> ![msg.payload.](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6b30f036d2292fe413279b1f841ad0ad6adf921.jpeg)

---

<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: [17 April 2020 00:17 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/6 "2020-04-17T00:17:45Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4d935cbcb6ad61d7d434832b23de989c6f99fc7.jpeg)  
PS, you are viewing element [12] not element [0]

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

---

<div class="post-metadata">

### Author: ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)
#### Post date: [17 April 2020 14:36 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/7 "2020-04-17T14:36:02Z")

</div>

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.

---

<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: [17 May 2020 14:36 UTC](https://discourse.nodered.org/t/msg-assignment-syntax-error/24982/8 "2020-05-17T14:36:09Z")

</div>

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