# HTTP request returns "undefined"

**URL:** https://discourse.nodered.org/t/http-request-returns-undefined/48455
**Category:** General
**Created:** [15 July 2021 13:09 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455 "2021-07-15T13:09:29Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [15 July 2021 13:09 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/1 "2021-07-15T13:09:29Z")

</div>

Hi,

I do a http request

```auto
https://mapper.packetbroker.net/api/v2/gateways/netID=000013,tenantID=ttn,id=farmspace-ug87-24e124fffef052a3

```

The response is

```auto
{"netID":"000013","tenantID":"ttn","id":"farmspace-ug87-24e124fffef052a3","eui":"24E124FFFEF052A3","clusterID":"ttn-eu1","updatedAt":"2021-07-15T12:56:28.092221Z","location":{"latitude":-27.9388491,"longitude":24.6428506,"altitude":1077,"accuracy":0},"antennaCount":1,"online":true,"frequencyPlan":{"region":"EU_863_870","loraMultiSFChannels":[868100000,868300000,868500000,867100000,867300000,867500000,867700000,867900000]},"rxRate":55.977493}

```

There are two fields value I want to wright to a DB - "rxRate" and "txRate".

Now some times the server responds with these fields missing or undefined, how can I handle this? If it is missing or undefinde I want to make the field = 0.

```auto
let rxRate = msg.payload.rxRate;
let txRate = msg.payload.txRate;

if(rxRate == undefined){
    let rxRate = 0
}

if(txRate == undefined){
    let txRate = 0
}

msg.payload = {
    eui: msg.payload.eui,
    online: msg.payload.online,
    rxRate: msg.payload.rxRate,
    txRate: msg.payload.txRate
}
'''
PS - I know my function is not close on correct.
```

---

<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: [15 July 2021 13:37 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/2 "2021-07-15T13:37:49Z")

</div>

> [@ScheepersJohan](#):
>
> ```auto
> if(rxRate == undefined){
> let rxRate = 0
> }
> 
> ```

Close, this should be better

```auto
if(typeof rxRate === "undefined"){
    rxRate = 0
}

```

And the same for the other variable. by using `let` again inside the if block you were defining a new variable of the same name, that was only valid inside the block. Comparing directly with `undefined` will generally work but is generally considered bad practice as `undefined` is effectively a global variable that can be overwritten.

In fact since you are setting the variables to `0` you can do  
`if (!rxRate) rxRate = 0`  
since an undefined variable is considered `false`. You could not use that technique if you were setting it to a non-zero value as if rxRate were already 0 then it would set rxRate to that non-zero value.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [15 July 2021 13:52 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/3 "2021-07-15T13:52:07Z")

</div>

I have update my code

```auto
let rxRate = msg.payload.rxRate;
let txRate = msg.payload.txRate;

if (!rxRate) rxRate = 0

if (!txRate) rxRate = 0

msg.payload = {
    eui: msg.payload.eui,
    online: msg.payload.online,
    rxRate: msg.payload.rxRate,
    txRate: msg.payload.txRate
}
delete msg.event;
delete msg.socketid;
delete msg.statusCode;
delete msg.responseUrl;
delete msg.redirectList;
delete msg.parts;
delete msg.headers;
delete msg.url;
return msg;

```

```auto
{"netID":"000013","tenantID":"ttn","id":"farmspace-7076ff0056070218","eui":"7076FF0056070218","clusterID":"ttn-eu1","updatedAt":"2021-07-15T13:40:30.197155Z","location":{"latitude":-27.9866033,"longitude":24.7272812,"altitude":1119,"accuracy":0},"antennaCount":1,"online":true,"frequencyPlan":{"region":"EU_863_870","loraMultiSFChannels":[868100000,868300000,868500000,867100000,867300000,867500000,867700000,867900000]}}

```

```auto
Error: A 400 Bad Request error occurred: {"error":"unable to parse 'ttnGateway eui=\"7076FF0056070218\",online=true,rxRate=undefined,txRate=undefined': invalid boolean"}

```

Now in this instance the two fields are not in the json, how do I handle that?

Thank you

---

<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: [15 July 2021 14:54 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/4 "2021-07-15T14:54:42Z")

</div>

> [@ScheepersJohan](#):
>
> ```auto
> msg.payload = {
> eui: msg.payload.eui,
> online: msg.payload.online,
> rxRate: msg.payload.rxRate,
> txRate: msg.payload.txRate
> }
> 
> ```

You haven't set the properties to your variables. It should be

```auto
msg.payload = {
    eui: msg.payload.eui,
    online: msg.payload.online,
    rxRate: rxRate,
    txRate: txRate
}

```

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [15 July 2021 15:06 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/5 "2021-07-15T15:06:34Z")

</div>

Sorry my apologies.

Will

```auto
if(typeof rxRate === "undefined"){
    rxRate = 0
}

```

handle when the field is missing?

---

<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: [15 July 2021 15:11 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/6 "2021-07-15T15:11:36Z")

</div>

> [@ScheepersJohan](#):
>
> handle when the field is missing?

If `msg.payload.rxRate` does not exist then it will be undefined, so the answer is yes.

Rather than delete all the properties you don't want (assuming you really do need to delete them, it is very rare to have to delete properties) then it would be better as

```auto

msg = {}
msg.payload = {
    eui: msg.payload.eui,
    online: msg.payload.online,
    rxRate: rxRate,
    txRate: txRate
}
return msg

```

That will give you a message with only the payload set.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [17 July 2021 10:30 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/7 "2021-07-17T10:30:56Z")

</div>

> [@Colin](#):
>
> `msg = {}`

Don't this delete the entire JSON?

How do you then have access to msg.payload.eui?

Or am I wrong?

---

<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 July 2021 10:35 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/8 "2021-07-17T10:35:51Z")

</div>

> [@ScheepersJohan](#):
>
> Don't this delete the entire JSON

Yes, however, strictly speaking msg is an object and it's properties are objects/strings/numbers etc. JSON is just a string format. So `msg = {}` actually means set msg to a new empty object. (sorry, not meaning to be pedantic)

For Colin's example to work, you'd have to store the properties `msg.payload.eui` and `msg.payload.online` in temporary variables before setting msg to a new empty object.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [17 July 2021 10:44 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/9 "2021-07-17T10:44:58Z")

</div>

That is what I mean, the payload objects/strings/numbers is deleted and you can not reuse them in the subsequent part of the code.

```auto
let eui = msg.payload.eui
let online = msg.payload.online
let rxRate = msg.payload.rxRate
let txRate = msg.payload.txRate

msg = {}

// Showing both ways to handle undefined object 
if (!rxRate) rxRate = 0

if(typeof txRate === "undefined"){
    txRate = 0
}

msg.payload = {
    eui: eui,
    online: online,
    rxRate: rxRate,
    txRate: txRate
}

return msg

```

---

<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: [17 July 2021 10:50 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/10 "2021-07-17T10:50:19Z")

</div>

Sorry, yes you are right. That should work, or

```auto
msg1 = {}
msg1.payload = {
    eui: msg.payload.eui,
    online: msg.payload.online,
    rxRate: rxRate,
    txRate: txRate
}
return msg1

```

---

<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 July 2021 10:50 UTC](https://discourse.nodered.org/t/http-request-returns-undefined/48455/11 "2021-07-31T10:50:26Z")

</div>

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