Trouble testing for undefined

Honestly, all my testing has been done without the 'node.warn', I just put that in there for this forums post thinking it would help show whats going on since the debug is of no value in this case.

The icao link works great. No problems with it at all.

Always correct, if msg.payload.length is zero it will drop through to the else condtion, where you then use msg.payload[0].type, but msg.payload[0] does not exist, so in the else condition you cannot use anything in msg.payload because there is nothing there.

Perhaps you need another test inside the first block of code (so there is something in the array). Having determined that there is something there then you should be able to use the hasOwnProperty test to check whether type is present.

1 Like

You could try a ternary operator to check if the array element is true.

msg.dts = new Date().toUTCString();

msg.payload = {
            Timestamp: msg.dts,
            Flight: msg.flight,
            Tail: msg.tail,
            Type: (msg.payload[0] ? msg.payload[0].type : ""),
            Desc: (msg.payload[0] ? msg.payload[0].description : ""),
            ICAOtxt: msg.aes,
            ICAO: msg.aes = "https://tar1090.adsbexchange.com/?icao="+ msg.aes,
            Raw : msg.raw
}
return msg;
1 Like

A general comment: In javascript, you often want to check if a property exists, for example you want to know if msg.payload.prop1.subprop1.subsubprop1 exists.
However, you cannot do msg.payload.prop1.subprop1.hasOwnProperty('subsubprop1'), if you cannot ensure that msg.payload.prop1.subprop1 exists.
Therefore, often you need to do things like
if(msg.hasOwnProperty('payload') && msg.payload.hasOwnProperty('prop1') && msg.payload.prop1.hasOwnProperty('subprop1')) and so forth. If you have an array in the mix, you can check for .length, Array.isArray() etc.

This will be simplified with ECMAScript 2020, which includes the ?. syntax

Colin suggests the same, but I thought I give a more detailed explanation.

@E1cid
Fantastic!
This is the only solution that is working.
I have not heard of a 'ternary operator', but will be sure to look into it.

Thanks everyone for your ideas and checks.

I use a small function:

isValidProperty(msg,['payload', 'type']

/** Validates whether property is safely accessable - empty string allowed
   * @param  {object} nestdObj object
   * @param  {array} path array with the property chain- should be non empty
   * @outputs {boolean} property exists
   */
  isValidProperty: (nestedObj, pathArray) => {
    const property = pathArray.reduce(
      (obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined),
      nestedObj
    )
    return module.exports.isTruthy(property)
  },

isTruthy is here (to avoid infinite, etc - you can simplify that)

isTruthy: input => {
    return !(typeof input === 'undefined' || input === null ||
      (typeof input === 'number' && !Number.isFinite(input)))
  },

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