Should assigning a value to a non-existent property give a run-time error?

I expected this code (in a function node) to generate a run-time error, but it doesn't.

msg.payload = 0
msg.payload.x = 7
return msg;

Do others find this surprising?

node-red 3.0.2
nodejs 16.17.0

By default, JavaScript doesn't throw an error when you try to assign a property to a type like number, boolean or string. It silently lets it fail.

If you enable strict mode, (add use strict to the top of the function) it will throw an error. Although I havent checked if that works inside the Function node.

OK, understood, thanks.

use strict throws a syntax error
"SyntaxError: Unexpected identifier (body:line 1)"

Are you actually doing this inside a function node? I have not been able get one to honor the "use strict" directive. (The monaco editor complains, of course.) What might I be doing wrong?

I meant that use strict throws the error.

Needs to be in inverted commas, I think?

'use strict'

msg.payload = 0
msg.payload.x = 7

return msg

Just returns 0 for me. As @drmibell says 'use strict' in a function node is ignored. I have never noticed this before

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