Trouble testing for undefined

Been at this for a few hours and have plumbed the depths of these forums, but I am still stuck.....

Doing a select on a MySQL database and if some of the results are missing from the database, I get a TypeError undefined.... so I need to check for it and simply replace the missing bits of data with something else (in my case "" will do).

Here are the tests I have used to try and detect the 'undefined' case;

//if (typeof msg.payload.type === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
///if (msg.payload.type.length === 0) {

I don't think I can post my flow here, since the database is needed.
So here is a screen shot of it working and not working.

The first two debug messages are the database returning everything.
The bottom two debug messages are the database not returning everything.

It seems that the error is happening BEFORE my code gets a chance at testing for it....

Put simply, how can I test for 'undefined' in this situation?

Could you not use -

Type: msg.payload[0].type || "",

Thanks for the reply, unfortunately it does not detect the undefined condition either.

I have added that test to comment section of tried, but not working.

Am I understanding correctly that you're looking at a situation where you have an object, and you're hoping to test whether the object has a certain key?
If so, have you tried for msg.payload.hasOwnProperty('type')?

Interesting. I had not come across that check before....
Here is a screen shot to make sure I have implemented it correctly.

If it is correct syntax, it does not work, still getting the undefined error.

I missed a [0] in your screenshot. What it does is it checks with the object you call it on (in my example above on msg.payload) for the existence of a property (or a key, if you will) named 'type'. So instead, try it with msg.payload[0].hasOwnProperty('type')

If it still isn't working feed the message you are trying to test into a debug node and show us what the message contains. I don't think you have done that so far, or if you did I did not understand what you were showing.

Your original debug screenshot does not show msg.payload as an array.

Getting a different error now.

Here is the flow.

The debug node does not show anything when it fails.
Only when the select statement works.

As I said in my first post, it seems that the 'undefined' error is happening before my code, so that's why none of the usual tests are working?

Put the debug node before your function so you can see what is going into the function.

I assume this is what you mean?

The select statement is going into the function correctly.

Since that aircraft is not in the database, it comes back empty.

OK, the payload is an empty array. So in your function you can use something like
if (msg.payload.length > 0) {
Whenever you have a node which is not doing what you expect the first thing to do is to check what is going into it so you know what you are dealing with.

1 Like

Thanks for your help.
As you can see from the comments mounting up at the top of the block, I have tried testing for a length of none.

Assuming I have the syntax correct, this also fails to detect the undefined condition.

Slight correction, you have tested for msg.payload.type.length to be 0. Not for the length of msg.payload. Testing for type.length would have for the number of characters, or if it was an array for the number of elements in the array, for msg.payload.type.

You are correct, my bad for not including it after testing it.

Here are the tests I have tried;

//if (typeof(msg.payload.type) === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
//if (msg.payload.type.length === 0) {
//if (msg.payload.length === 0) {
//if (msg.payload.type || "") {
//if (msg.payload[0].type || "") {
//if (msg.payload.hasOwnProperty('type')) {
//if (msg.payload.length > 0) {

Please post the full function, with the msg.payload.length test so we can see what else is going on in the function. Copy the source please, go to a new line and click the </> button and paste it in there.

1 Like

Here is the full code in the function block.

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

node.warn("Raw "+ msg.payload);
//if (typeof(msg.payload.type) === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
//if (msg.payload.type.length === 0) {
//if (msg.payload.length === 0) {
//if (msg.payload.type || "") {
//if (msg.payload[0].type || "") {
//if (msg.payload.hasOwnProperty('type')) {
//if (msg.payload.length > 0) {

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



return msg;

If array is empty msg.payload[0].description will not be defined also.

Sometimes correct.
Some aircraft have types and descriptions and some have different combinations.
Since I never get to test for descriptions, I have not worried about that check yet... Once I get the missing 'type' I can work on any missing 'descriptions'.

This line will cause a string concatenation to happen, it will take msg.payload and get the toString() version of it and adds it to the string "Raw ". If instead you were to use it for debugging, try warning directly with msg.payload. When you see Raw [object object] in the debug sidebar, it's because msg.payload is an object, and toString of an object results in the string representation of [Object object].

Furthermore, this line ICAO: msg.aes = "https://tar1090.adsbexchange.com/?icao="+ msg.aes, will likely not do what you think it will.