Null instead of undefined

Good day.

const set = [ ];
set.push (undefined)
msg.payload = [... set];
return msg;

the result is null
msg.payload: array [1]
[null]
Why not undefined?

The same code in the browser:
const set = [ ];
set.push (undefined)
1
[... set]
[undefined]

Regards, Yuri.

Hi Daneev welcome to the forum.

I'll assume ou are doing this in a function node, so could you copy and paste the full example of each case.

When you paste in the code please use the </> icon arround your code to preserve the formatting.

Thanks!

[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = [];\nset.push(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":1420,"wires":[["d9c498c9.9c4988"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1420,"wires":[]}]

This appears to be a bug in the debug node output, which shows null for an array element that is actually undefined.

Probably it is. But can this be fixed?

[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = new Set();\nset.add(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":1420,"wires":[["d9c498c9.9c4988"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1420,"wires":[]}]

with sets the same way.

What do you mean by fixed? It is only the debug output that is wrong. Just ignore it.

I would suggest to report it as an issue as it sounds like an obvious bug. You can report issues in the GitHub page: https://github.com/node-red/node-red

These are different types of values. I can't help but pay attention.

Yes, but it does not affect the operation of the flow. As @ristomatti has suggested, file an issue and then just ignore the debug output (or disable that debug node).

[{"id":"4290dec2.c896d","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1420,"wires":[["ffef1f99.e3147"]]},{"id":"ffef1f99.e3147","type":"function","z":"b51d87d0.c86408","name":"","func":"var set = new Set();\nset.add(undefined);\nmsg.payload = [...set];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":1420,"wires":[["a03fb66f.8b2a68"]]},{"id":"d9c498c9.9c4988","type":"debug","z":"b51d87d0.c86408","name":"yes","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":1380,"wires":[]},{"id":"a03fb66f.8b2a68","type":"switch","z":"b51d87d0.c86408","name":"is of type undefined?","property":"payload","propertyType":"msg","rules":[{"t":"istype","v":"undefined","vt":"undefined"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":600,"y":1420,"wires":[["d9c498c9.9c4988"],["d0a30f32.cf1b3"]],"outputLabels":["yes","no"]},{"id":"d0a30f32.cf1b3","type":"debug","z":"b51d87d0.c86408","name":"no","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":1460,"wires":[]},{"id":"a17fdd37.62873","type":"inject","z":"b51d87d0.c86408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1480,"wires":[["5afb409a.c968b"]]},{"id":"5afb409a.c968b","type":"function","z":"b51d87d0.c86408","name":"undefined","func":"node.send({payload: undefined});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":1480,"wires":[["a03fb66f.8b2a68"]]}]

affects work too

I seem to recall this is due to the fact we need to serialise the data to send it over the web socket to the browser, and it is not possible to represent undefined in any meaningful way, so it ends up being a null. I don’t think there is a way to fix this until there is a way to create a serialisable representation of undefined.

1 Like

I gave an example just above. there you can see that the error at the output of the function can affect the further logic of the process.

Out of curiosity I verified the issue and that it only affects the debug bar:

Both function nodes console.log(msg.payload) and debug node is set to output both to debug tab and its status.

@dceejay Status seems unaffected although it's part of the editor. But I guess that's just a JSON.stringify output and the debug tab shows actual (first serialized) objects?

(Sorry about the long screenshot, couldn't easily get a better one on my tablet)

Your switch node is looking for msg.payload undefined. msg.payload is not undefined it is an array containing one element which is itself undefined.

1 Like

Yes the status is already a string by the time it is sent.

Yes, I'm sorry, I overlooked.
this is how it works
msg.payload = set [0];

This is how what works? Is your problem now solved?

I agree, already convinced of this. but it doesn't look pretty.

I noticed a single property containing undefined (e.g. msg.payload[0] in this case prints fine. The serialisation works with them or there's some special handling?

I also tried sending

msg.payload = {foo: undefined, bar: null};

In this case the debug output was not

{
  foo: null,
  bar: null
}

as I expected but instead

{
  bar: null
}

Interesting issue indeed. Good to know about it.