Split not supported in function node

Hi,
I was trying the solution proposed in this post, to convert a numeric array into char:

But when I try Nick's solution, seems that the split method is not supported:
image

Anyone has an idea why?

I'm using NodeRed 3.1.9

BR

F.

Msg.payload seems to be an array already so you would not require a split.
Just use the map
msg.payload = msg.payload.map(v => parseInt(v))

[edit]
I also just noticed that your array seems to be integers already, so would also not require to be parsed to integers.
So you can go straight to the
msg.payload = Buffer.from(msg.payload).toString();

1 Like

Education:

split creates an array, using a delimiter to identify the elements (example : ,) , but as @E1cid pointed out - you already have an array (already split)

Your data is a buffer. You can either use the buffer-parser node (install from palette) or a function with ...

msg.payload = msg.payload.toString()
return msg
1 Like

Yeah, but I don't simply want to shows those bytes as string. I actually want to convert the numeric values to their string equivalent, so that I can display them with a text node. If I simply do a 'toString' conversion i see just garbage (not printable ASCII or no meaning stuff).

Hope I made myself clear...

Still not crystal.
but try

msg.payload = [...msg.payload].map(asi => String.fromCharCode(asi))

should return

["\u000b","\t","","I","Ƙ"]

when feed your buffer.
or

msg.payload = String.fromCharCode(...msg.payload)

if you do not want an array.

To show them in "a text node" implies 1 string.

So what exactly should this string contain?

  • [11, 5, 127, 73, 216]
  • 11, 5, 127, 73, 216
  • 11 5 127 73 216
  • 11512773216
  • Something else?

Please be clear.

Hi,
Apologize for the unclear explanation. Both bullet points 2 and 3 are fine. The bytes in the array are received from an uart channel and are part of a specific communication protocol.

Anyhow, speaking with a friend much mooooore confident with JavaScript than me, I've found the line of code that does what I need:

msg.payload = msg.payload.map(i =>i.toString()).join(" ")

Using this in a function node does the conversion I need to correctly visualize the array in a dashboard text node.

Thanks everyone
F.

So now we know more prcisely, then you do not need to map or toString just
msg.payload = [...msg.payload].join(" ");
should do what you wish.

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