Creating arrays in a function

Hi
I’m newish so be gentle.
I am struggling with a function that creates an array with msg.payload, having been initiated by injecting a random value (doesn’t matter what).

Using the following:


msg.payload[0] = 1;
msg.payload[1] = 2;
Return msg;

(Values or type that you set doesn’t matter)
The function just passes through the injected msg.
I can obviously modify msg.payload or insert objects but it just won’t create that array.
It has worked on other functions but being new, perhaps there is a subtle difference I am missing.

Cheers
Nick

You inject a number (timestamp) from an inject node I presume.
So msg.payload is now a number, not an array. So you would need to set it as an array in your function code first
e.g.
msg.payload = []; initialise an empty array

Then you can do
msg.payload[0] = 1;

If you injected a json of value [ ] it would of worked also, as that would have set msg.payload to an empty array.

Or
msg.payload = [1,2]
return msg

Thank you both, it is obvious now that you kindly pointed out my error.
Some rust has been knocked off that part of rhe brain.
Much appreciated.

dB