Disregard "undefined" msg

In a normal situation my payload is like this:
1:13.5230:14.0900:0.0000:0.0000:24:24:216:216

Time to time I get back messages like this:

Cells : msg : Object
object
payload: object
NaNVCNaN: undefined
NaNTCNaN: undefined
_msgid: "01b9257b73bb0ea0"
topic: "Cells"

I did try to filter out such type of messages using:

if(isNaN(msg.payload)) {
  return null; //halt flow
}
return msg;

and

if(msg.payload=="") {
  return null; //halt flow
}
return msg;

but are still going trough.
How do I disregard such undefined messages?

They wont work as payload is an object ...

... so will never be caught by these tests.


Assuming your payload is NORMALLY a string as per your example above, you could change your "filter out" tests to ...

if(typeof msg.payload !== "string" || msg.payload == null) {
  return null; // halt flow because payload is NOT a string or is empty
}

Thanks for you help.
My normal msg.payload is like this:

Schermata 2022-04-07 alle 11.13.10

Despite the filter function I tried and even the one you suggested, at time this is still going trough:

Schermata 2022-04-07 alle 11.22.24

Try

if (msg.payload == "" || msg.payload == null) {
  return null; // halt flow because payload is an empty string
}

ok, so your normal payload is a string. So the suggestion I made should get you closer to a solution as it will discard empty strings and non strings...



Looking at this :point_up_2:screenshot, it seems payload has (potentially) a newline character in it. so you could trim it...

if(typeof msg.payload !== "string" || msg.payload.trim() == "") {
  return null; // halt flow because payload is NOT a string or is empty
}

Thank you but unfortunately this doesn't work in my case

Thank you. Problem solved.

You can also use a switch node and filter all message which are not null. No need to use a function node.

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