# Again get data from a payload

**URL:** <https://discourse.nodered.org/t/again-get-data-from-a-payload/80800>\
**Category:** General\
**Created:** [25 August 2023 16:00 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800 "2023-08-25T16:00:56Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [27 August 2023 12:40 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/21 "2023-08-27T12:40:24Z")

</div>

> [@Colin](#):
>
> var prognose = Object.values(msg.payload.result)

TypeError: Cannot convert undefined or null to object

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 August 2023 13:00 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/22 "2023-08-27T13:00:23Z")

</div>

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

That isn't what I said.

---

<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:** [27 August 2023 13:03 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/23 "2023-08-27T13:03:57Z")

</div>

To unserstand what is going on, we need all relavint info at the same time.

change your function to this ↓ and show is what you get in the debug panel

```auto
if (typeof msg.payload !== 'object') {
    node.error('expected msg.payload to be an object')
    return
}

const resultArray = Object.values(msg.payload.result)
if (Array.isArray(resultArray ) && resultArray.length >= 2) {
    msg.payload = resultArray[1]
    return msg;
} else {
    node.error('Object.values(msg.payload.result) is not an array or does not have 2 or more elements')
}

```

---

<div class="post-metadata">

**Author:** ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)\
**Post date:** [27 August 2023 13:08 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/24 "2023-08-27T13:08:38Z")

</div>

OK folks, I'll try it now in german:

```auto
[{"id":"d686e0ddf37b8661","type":"inject","z":"5a1cda5786c898fd","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":120,"wires":[["99e86353658eee4b"]]},{"id":"99e86353658eee4b","type":"function","z":"5a1cda5786c898fd","name":"function 2","func":"var test = {\n payload:\n {\n result:\n {\n \"2023-08-27\": 9996787,\n \"2023-08-28\": 11724368\n }\n }\n};\n\nreturn test;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":120,"wires":[["df3c8905088381de","6ebdec126ab41e03"]]},{"id":"df3c8905088381de","type":"debug","z":"5a1cda5786c898fd","name":"debug 40","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":80,"wires":[]},{"id":"6ebdec126ab41e03","type":"function","z":"5a1cda5786c898fd","name":"function 3","func":"msg.payload = Object.values(msg.payload.result)[1];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":140,"wires":[["0998ab2bc1c304a0"]]},{"id":"0998ab2bc1c304a0","type":"debug","z":"5a1cda5786c898fd","name":"debug 41","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":860,"y":140,"wires":[]}]

```

Das ist ein ganz einfacher Flow mit einem Inject, 2 Funktions-Nodes und 2 Debugs.  
[Edit] Der erste Funktions-Node simuliert Deine Daten, der zweite extrahiert das gewünschte Ergebnis.

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

Der erste Debug zeigt genau Deine Struktur, der zweite genau das gewünschte Ergebnis.

Du musst an irgendeiner anderen Stelle etwas falsch machen!?

---

<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:** [27 August 2023 13:48 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/25 "2023-08-27T13:48:09Z")

</div>

try what I offered.

> [@Steve-Mcl](#):
>
> ```auto
> if (typeof msg.payload !== 'object') {
> node.error('expected msg.payload to be an object')
> return
> }
> 
> const resultArray = Object.values(msg.payload.result)
> if (Array.isArray(resultArray ) && resultArray.length >= 2) {
> msg.payload = resultArray[1]
> return msg;
> } else {
> node.error('Object.values(msg.payload.result) is not an array or does not have 2 or more elements')
> }
> 
> ```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55ed5de27bd5cfab9fff4f95bf59b819fb2303bb.png)

---

<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:** [27 August 2023 13:49 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/26 "2023-08-27T13:49:37Z")

</div>

As you can see it works. The issue you might have is unexpected payloads hitting the function. The code I offered you will check everything is as you expect & will let you know if it is not!

---

<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:** [27 August 2023 14:57 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/27 "2023-08-27T14:57:05Z")

</div>

Ok the output of the solar forcast node on the second output is

```auto
[{"labels":["",""],"data":[],"series":[]}]

```

You want the output of the first output

```auto
{"result":{"2023-08-27":16313510,"2023-08-28":10678228},"message":{"code":0,"type":"success","text":"","pid":"164i6btQ","info":{"latitude":51.0769,"longitude":6.2243,"distance":0,"place":"Erkelenzer Straße 16C, 41836 Hückelhoven, Germany","timezone":"Europe/Berlin","time":"2023-08-27T16:51:16+02:00","time_utc":"2023-08-27T14:51:16+00:00"},"ratelimit":{"period":3600,"limit":12,"remaining":11}}}

```

So the function works but you are feeding it the wrong output

 ![chrome_screenshot_1693148035315](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c675ab2955800ec143d75f997848b11264dca886.png)

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [27 August 2023 16:40 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/28 "2023-08-27T16:40:26Z")

</div>

sorry colin, i have test it. The same error .... after that i make the changes back .... and then i get the screenshot

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [27 August 2023 16:42 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/29 "2023-08-27T16:42:49Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> if (typeof msg.payload !== 'object') {
> node.error('expected msg.payload to be an object')
> return
> }
> 
> const resultArray = Object.values(msg.payload.result)
> if (Array.isArray(resultArray ) && resultArray.length >= 2) {
> msg.payload = resultArray[1]
> return msg;
> } else {
> node.error('Object.values(msg.payload.result) is not an array or does not have 2 or more elements')
> }
> 
> ```

sorry the same error!

---

<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:** [27 August 2023 16:46 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/30 "2023-08-27T16:46:44Z")

</div>

> [@einfach112](#):
>
> sorry the same error

I doubt that. Show us the debug output

---

<div class="post-metadata">

**Author:** ![einfach112](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@einfach112](https://discourse.nodered.org/u/einfach112)\
**Post date:** [27 August 2023 16:46 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/31 "2023-08-27T16:46:48Z")

</div>

> [@E1cid](#):
>
> So the function works but you are feeding it the wrong output

OMG ! SORRY  
I have copy the same node above with other functions  
OMG ! SORRY Thank you everyone !

---

<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:** [10 September 2023 16:47 UTC](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800/32 "2023-09-10T16:47:17Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/again-get-data-from-a-payload/80800.md?page=1)
