# Msg.length is returning an empty object as a result?

**URL:** <https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136>\
**Category:** General\
**Created:** [31 October 2020 22:51 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136 "2020-10-31T22:51:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwking11](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gwking11](https://discourse.nodered.org/u/gwking11)\
**Post date:** [31 October 2020 22:51 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/1 "2020-10-31T22:51:34Z")

</div>

Hey all,

I am new to Node-Red and am running it in Home Assistant. I am also new to HTML with very little previous programming knowledge(some Basic/Visual Basic/Access/C++). Yes that says Basic.

I am pulling an https request from [OpenWeatherMap.org](http://OpenWeatherMap.org) that is returning an array of objects(i think?). Is there a way to iterate through all the elements of all the objects and round all numbers to no decimals?

I tried:

```auto
var x = 0;
var len = {payload: msg.payload.length };

while (x < len) {
	if (typeof msg.payload[X]	!== 'string'){
		msg.payload[x] = Number(a.toFixed(0))
	}
x++
}

```

which did not work.

I then tried to set msg.payload.len to the length function with:

```auto
msg.payload.len = {payload: msg.payload.length };

```

and I get this:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0ca5f3946a01bcf8b5bf5415825f73d904ee349a.png)

As you can see I have .len shown as an object under msg.payload and it shows empty in the debug window. I have tried with and without the () after .length.

Any help appreciated. All of my searches give me nothing except that {payload: msg.payload.length} should work. I tried msg.length as well.

George

---

<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:** [31 October 2020 23:15 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/2 "2020-10-31T23:15:08Z")

</div>

Looks like a object of objects.

```auto
msg.payload.len = {payload: Object.keys(msg.payload).length };

```

To be sure an example of the return object from the api request would help.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 October 2020 23:22 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/3 "2020-10-31T23:22:49Z")

</div>

I am playing with the node `openweathermap` myself.

It may be a good idea to get that node and use it to parse/create a message for the place you want to get the weather.

Though, if you want to do it that way, I would open all the messages and point to the exact data you want to see/use.

---

<div class="post-metadata">

**Author:** ![gwking11](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gwking11](https://discourse.nodered.org/u/gwking11)\
**Post date:** [31 October 2020 23:26 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/4 "2020-10-31T23:26:48Z")

</div>

Wow thanks for the quick response.

That does return the total number of the objects in the .payload. It return 16 which is the first level of objects, but how do would I make it look inside each object also?

Here is the list from debug:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea53f66359e4350591f55dce8c6a040a6394d8f1.png)

I would like to not only like to have "day1" counted, but also the items listed under "day1."

Any Suggestions?

---

<div class="post-metadata">

**Author:** ![gwking11](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gwking11](https://discourse.nodered.org/u/gwking11)\
**Post date:** [31 October 2020 23:28 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/5 "2020-10-31T23:28:30Z")

</div>

I did not realize there was a node for OpenWeatherMap. Do you have the location for the repository?

Thanks,

George

---

<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:** [31 October 2020 23:29 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/6 "2020-10-31T23:29:44Z")

</div>

Yes if you supply an example of the object, there are 6 different objects the api can return, depending on the request.

A screenshot does no work i can not copy or paste the info.

Copy the value and post the result between backticks.  
i.e.  
```  
value  
```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 October 2020 23:31 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/7 "2020-10-31T23:31:14Z")

</div>

> **[node-red-node-openweathermap](https://flows.nodered.org/node/node-red-node-openweathermap)**
>
> A Node-RED node that gets the weather report from openweathermap

Just go to the library and enter `openweathermap`.

You will need to _register_ and get a key, but that isn't a big deal.  
I'm security conscious and it wasn't a problem for me.

---

<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:** [31 October 2020 23:31 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/8 "2020-10-31T23:31:52Z")

</div>

open pallette and search "openweathermap"

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 October 2020 23:34 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/9 "2020-10-31T23:34:12Z")

</div>

You are getting a multi-day response.

Not a big deal.

Each day will be structured the same. Just the data will be slightly different in _where_ it is.

In you example is would be something like:  
`msg.day1......`  
And you would have the `sunrise` and `sunset` times.  
Then you would have `temp` which would have a sub-set of `day` `min` `max` `night` and `eve`.... etc.  
Then the `feels_like` with it's sub set.

Day 2 would be only a case of:  
`msg.day2.......`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 October 2020 23:36 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/10 "2020-10-31T23:36:30Z")

</div>

(Well, you need to click on the `install` tab rather than the default `nodes` tab.)

---

<div class="post-metadata">

**Author:** ![gwking11](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gwking11](https://discourse.nodered.org/u/gwking11)\
**Post date:** [1 November 2020 17:02 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/11 "2020-11-01T17:02:52Z")

</div>

> [@Trying\_to\_learn](#):
>
> Just go to the library and enter `openweathermap`

Ok I did this. I had to install npm in order to install the node. When I installed it seems to have gone off without a hitch, but the nodes were not added to my node-red window?

Here was the response from the install

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/17d7471c20c49c36026e67e38e3d63e92d341cef.png)

I restarted HASS after the install and closed and reloaded the web gui, still no new nodes...

Any Ideas?

---

<div class="post-metadata">

**Author:** ![gwking11](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gwking11](https://discourse.nodered.org/u/gwking11)\
**Post date:** [1 November 2020 17:10 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/12 "2020-11-01T17:10:20Z")

</div>

oh I see now. sorry new to all this. I figured it out

Thanks for the help

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 November 2020 19:38 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/13 "2020-11-01T19:38:18Z")

</div>

I think you need to be in ~/.node-red to install the node.

And you could have done it from within node-red. Menu, palette manager, install and type the name.

---

<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:** [31 December 2020 19:38 UTC](https://discourse.nodered.org/t/msg-length-is-returning-an-empty-object-as-a-result/35136/14 "2020-12-31T19:38:19Z")

</div>

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