HTTP request returns "undefined"

Hi,

I do a http request

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

The response is

{"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.

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.

Close, this should be better

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.

1 Like

I have update my code

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;
{"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]}}
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

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

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

Sorry my apologies.

Will

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

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


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.

Don't this delete the entire JSON?

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

Or am I wrong?

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.

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.

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

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

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

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