Iterate over element of msg.payload and check for nulls

Hi - I have this requirement - I have the msg.payload with 37 + properties, and I want to check them for nulls before I move on in the flow. Attaching a sample payload screenshot, but you can assume any object for references.

There is only one payload item, and not many. How do I go about doing this now. If I use the split node, by default I end up in only one object - but i just need iteration over the 37 properties in one go just for checking if they contain values or not.

So if any one property is null do you ignore the complete msg or remove that property?

In a function...

//test the payload object for nulls...
var hasNullOrUndefined = Object.entries(msg.payload).some(e => (e[1] == null));

//check if we found nulls/undefined?
if (hasNullOrUndefined) {
    return null; //halt the msg (dont pass it to next node)
}

return msg; //payload is ok - return the msg.

Explanation

Object.entries(msg.payload)

Object.entries turns the object into an array of 2 element arrays. The inner arrays each contains the property name in [0] and the value in [1].


.some(e => (e[1] == null));

some() checks an array & returns true if 1 item matches the criteria. In this case, the criteria is checking to see if the inner array element [1] (the property value) is null.

Yes, need to ignore the message

In that case @Steve-Mcl example is your answer.

1 Like

Let me try this out and let you know

Thanks - this worked, however, some values are empty - i did an additional condition for the code.
This does not seem to work well, as I can see the blank values still in the outputs. Appreciate your support

//test the payload object for nulls...
var hasNullOrUndefined = Object.entries(msg.payload).some(e => (e[1] === null));

// test the payload for blank entries

var isEmpty = Object.entries(msg.payload).some(e => (e[1] === ''));

//check if we found nulls/undefined?
if (hasNullOrUndefined || isEmpty) {
    return null; //halt the msg (dont pass it to next node)
}
else
    return msg; //payload is ok - return the msg.```

By testing for === null you will not detect undefined.

All you need is...

var hasNullOrUndefined = Object.entries(msg.payload).some(e => (e[1] == null || e[1] === ""));

Tried both the options.
However, the output still has blanks.

Can you capture one of the messages that this happens with?

Put a debug before the function, use the copy value button that appears under you mouse cursor when you hover over the debug message in the side bar.

The screenshots as attached.
I have several signals directly from the machine, and using the join node to create my payload. When I put the debug before the checks, then the message is not built properly as such in one go is what I can see. Let me know if this is the right approach to do it.

What is wrong with just putting a debug node on the output of the join? That's after the payload is built & before the function right?

Essentially, I can't copy/paste/test code from a screen shot. You need to post the message exactly as I requested.

An I missing something?

Sorry - forgot to paste the message. After the join node now, The result from msg.payload is

ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT: 231
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT: 241.1999969482422
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT: 241.1999969482422
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT: 241.1999969482422
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT: 220
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT: 220
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT: 220
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT: 220
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT: 231
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT: 231
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT: 231
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT: 13
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT: 13.5
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT: 13.5
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT: 13
ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT: 241.1999969482422
ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate: "05\06\2013"
ns=2;s=C08.C08$T.Codes.OperatorCode: "638998.opt"
ns=2;s=C08.C08$T.Codes.OrderCode: "MOK-091020-010$001 "
ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT: 241.22000122070312
ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT: 241.25999450683594
ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT: 219.92999267578125
ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT: 220
ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT: 231.10000610351562
ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT: 231.0800018310547
ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT: 17.09000015258789
ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT: 17.020000457763672
ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT: 241.27000427246094
ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT: 241.3000030517578
ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT: 219.9600067138672
ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT: 219.97999572753906
ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT: 231.11000061035156
ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT: 231.08999633789062

However, when I do the null checks and then pass the message, get many blanks as well. Will post the entire flow in 10 minutes for referal

flows (7).json (20.4 KB)

The flow for this project. Essentially, all inject elements at the join should have values - this is the check I am looking out for.

Did you get that by pressing the "copy value" button that appears under you mouse as I explained before?

Yes - To be precise - this was the message.

{"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT":231,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT":241.1999969482422,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT":241.1999969482422,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT":241.1999969482422,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT":220,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT":220,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT":220,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT":231,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT":231,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT":13.5,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT":13.5,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT":13,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT":220,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT":231,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT":13,
"ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate":"05\\06\\2013",
"ns=2;s=C08.C08$T.Codes.OperatorCode":"638998.opt",
"ns=2;s=C08.C08$T.Codes.OrderCode":"MOK-091020-010$001\r",
"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT":241.22000122070312,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT":241.25999450683594,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT":219.92999267578125,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT":220,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT":231.10000610351562,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT":231.0800018310547,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT":17.09000015258789,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT":17.020000457763672,
"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT":241.1999969482422,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT":241.27000427246094,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT":241.3000030517578,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT":219.9600067138672,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT":219.97999572753906,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT":231.11000061035156,
"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT":231.08999633789062}

Thats better.

OK, so proof that it works...
image

demo flow

[{"id":"60d1f2be.92678c","type":"inject","z":"b872cb4b.5a6448","name":"ok data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT\":231, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT\":241.1999969482422, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT\":241.1999969482422, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT\":241.1999969482422, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT\":220, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT\":220, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT\":220, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT\":231, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT\":231, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT\":13.5, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT\":13.5, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT\":13, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT\":220, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT\":231, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT\":13, \"ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate\":\"05\\\\06\\\\2013\", \"ns=2;s=C08.C08$T.Codes.OperatorCode\":\"638998.opt\", \"ns=2;s=C08.C08$T.Codes.OrderCode\":\"MOK-091020-010$001\\r\", \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT\":241.22000122070312, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT\":241.25999450683594, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT\":219.92999267578125, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT\":220, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT\":231.10000610351562, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT\":231.0800018310547, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT\":17.09000015258789, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT\":17.020000457763672, \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT\":241.1999969482422, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT\":241.27000427246094, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT\":241.3000030517578, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT\":219.9600067138672, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT\":219.97999572753906, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT\":231.11000061035156, \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT\":231.08999633789062}","payloadType":"json","x":330,"y":800,"wires":[["d746bfc8.ae662"]]},{"id":"d746bfc8.ae662","type":"function","z":"b872cb4b.5a6448","name":"Check data","func":"//test the payload object for nulls...\nvar hasNullOrUndefined = Object.entries(msg.payload).some(e => (e[1] == null || e[1] === ''));\n\n//check if we found nulls/undefined?\nif (hasNullOrUndefined) {\n    let badEntries = Object.entries(msg.payload).filter(e => (e[1] == null || e[1] === ''))\n    node.warn({ \"WARN\": \"Found null or empty values\", badEntries: badEntries});\n    return null; //halt the msg (dont pass it to next node)\n}\nelse {\n    return msg; //payload is ok - return the msg.\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":800,"wires":[["d367110e.ca1bf"]]},{"id":"d367110e.ca1bf","type":"debug","z":"b872cb4b.5a6448","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":800,"wires":[]},{"id":"b3b36d58.57ffe","type":"inject","z":"b872cb4b.5a6448","name":"bad data (null value)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT\":13.5,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT\":13.5,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT\":13,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT\":null,\"ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate\":\"05\\\\06\\\\2013\",\"ns=2;s=C08.C08$T.Codes.OperatorCode\":\"638998.opt\",\"ns=2;s=C08.C08$T.Codes.OrderCode\":\"MOK-091020-010$001\\r\",\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT\":241.22000122070312,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT\":241.25999450683594,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT\":219.92999267578125,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT\":220,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT\":231.10000610351562,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT\":231.0800018310547,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT\":17.09000015258789,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT\":17.020000457763672,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT\":241.1999969482422,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT\":241.27000427246094,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT\":241.3000030517578,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT\":219.9600067138672,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT\":219.97999572753906,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT\":231.11000061035156,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT\":231.08999633789062}","payloadType":"json","x":370,"y":840,"wires":[["d746bfc8.ae662"]]},{"id":"f267dc8b.f89b","type":"inject","z":"b872cb4b.5a6448","name":"bad data (empty string)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT\":241.1999969482422,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT\":13.5,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT\":13.5,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT\":13,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT\":220,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT\":231,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT\":123,\"ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate\":\"05\\\\06\\\\2013\",\"ns=2;s=C08.C08$T.Codes.OperatorCode\":\"\",\"ns=2;s=C08.C08$T.Codes.OrderCode\":\"MOK-091020-010$001\\r\",\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT\":241.22000122070312,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT\":241.25999450683594,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT\":219.92999267578125,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT\":220,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT\":231.10000610351562,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT\":231.0800018310547,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT\":17.09000015258789,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT\":17.020000457763672,\"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT\":241.1999969482422,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT\":241.27000427246094,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT\":241.3000030517578,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT\":219.9600067138672,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT\":219.97999572753906,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT\":231.11000061035156,\"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT\":231.08999633789062}","payloadType":"json","x":380,"y":880,"wires":[["d746bfc8.ae662"]]},{"id":"99c8d108.39792","type":"inject","z":"b872cb4b.5a6448","name":"bad data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":295,"y":920,"wires":[["bdc8210c.28e46"]],"l":false},{"id":"bdc8210c.28e46","type":"function","z":"b872cb4b.5a6448","name":"bad data (undefined)","func":"msg.payload = {\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_RIGHT\": 231,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_RIGHT\": 241.1999969482422,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_LEFT\": 241.1999969482422,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_2_PreSeries_RIGHT\": 241.1999969482422,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_RIGHT\": 220,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_LEFT\": 220,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_2_PreSeries_RIGHT\": 220,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_LEFT\": 231,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_2_PreSeries_RIGHT\": 231,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_RIGHT\": 13.5,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_RIGHT\": 13.5,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_2_PreSeries_LEFT\": 13,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureB_1_PreSeries_LEFT\": 220,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureC_1_PreSeries_LEFT\": 231,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureD_1_PreSeries_LEFT\": 123,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MasterDate\": \"05\\\\06\\\\2013\",\n    \"ns=2;s=C08.C08$T.Codes.OperatorCode\": \"fred\",\n    \"ns=2;s=C08.C08$T.Codes.OrderCode\": \"MOK-091020-010$001\\r\",\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_LEFT\": 241.22000122070312,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_1_Approval_RIGHT\": 241.25999450683594,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_LEFT\": 219.92999267578125,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_1_Approval_RIGHT\": 220,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_LEFT\": 231.10000610351562,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_1_Approval_RIGHT\": 231.0800018310547,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_LEFT\": 17.09000015258789,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureD_1_Approval_RIGHT\": 17.020000457763672,\n    \"ns=2;s=C08.C08$T.PreSeries Measurements.MeasureA_1_PreSeries_LEFT\": 241.1999969482422,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_LEFT\": 241.27000427246094,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureA_2_Approval_RIGHT\": undefined,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_RIGHT\": 219.9600067138672,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureB_2_Approval_LEFT\": 219.97999572753906,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_LEFT\": 231.11000061035156,\n    \"ns=2;s=C08.C08$T.Approval Measurements.MeasureC_2_Approval_RIGHT\": 231.08999633789062\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":920,"wires":[["d746bfc8.ae662"]]}]

the function...

//test the payload object for nulls...
var hasNullOrUndefined = Object.entries(msg.payload).some(e => (e[1] == null || e[1] === ''));

//check if we found nulls/undefined?
if (hasNullOrUndefined) {
    let badEntries = Object.entries(msg.payload).filter(e => (e[1] == null || e[1] === ''))
    node.warn({ "WARN": "Found null or empty values", badEntries: badEntries});
    return null; //halt the msg (dont pass it to next node)
}
else {
    return msg; //payload is ok - return the msg.
}

If you are still getting a message with an empty item, can you now capture the payload just like you did last time & reply with the "bad data"

Here is a slightly more verbose version that does extra checks and is easier to maintain / add checks to...

//a test function to determine if value is bad
var isValueNullOrEmpty = function(value) {
    if (typeof value === "undefined") return true;
    if (value == null) return true;
    if (typeof value == "string") {
        if (value.trim() === "") return true;
    }
    if (typeof value == "number") {
        if (isNaN(value)) return true;
    }
    return false;
}

//get the payload object properties with bad values...
let badEntries = Object.entries(msg.payload).filter(e => isValueNullOrEmpty(e[1]))

//check if we found nulls/undefined?
if (badEntries && badEntries.length) {
    node.warn({ "WARN": "Found null or empty values", badEntries: badEntries});
    return null; //halt the msg (dont pass it to next node)
}

//payload is ok - return the msg.
return msg; 

Thanks much - will try out.

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