Message payload - String?

I have been using NodeRed for quite a while and have never seen this before. I am using the "file" in node to grab the contents of a file and it seems to work fine, but it's outputting the message payload as a "string" in a way I have never seen before


I have a switch node after the file in and when doing a payload == some_value it does not work, if I use "contains" it works but that is not ideal in this situation, plus I would like to understand what this is. I have never seen a payload be marked as "string" this way. Any help would be greatly appreciated!

1 Like

That means it contains a newline character. If you click on it again to collapse back down, it'll look something like this:

You can see the newline meta character is displayed at the end.

This is why you can't do a direct comparison - you'll need to strip off the newline before you can do that.

Oh wow yeah that was the problem, thank you so much! What would be the easiest way to strip return off the end, I have never had to deal with this one before.

Have a look at the trim() method:

Alternatively you could leave it there and use a regex compare to look for something like ^yo\s$ (untested) which matches start of line followed by yo then one white space character and then end of line (I think, I always have to play about a bit to get regexs to work as I don't use them very often).

I always found to be a good place to fiddle with regex expressions. I think the carriage return which is the problem here is \r in a regex if your trying to build one.
Happy forum anniversary by the way @Colin

\s should match CR or LF (or other white space) so saves trying to work out which symbol is which.

Thanks. :slight_smile:

1 Like

Sometimes it can be stubborn; to remove the return or next line I keep this in my notes file.

Function to remove next line:

msg.payload = msg.payload.replace(/\n/g,"");
return msg;

Function to remove Return:

msg.payload = msg.payload.replace(/\r/g,"");
return msg;

You can remove both too!:

msg.payload = msg.payload.replace(/[\r\n]/g, "");
return msg;

this is what trim() does, no need for a regex replace.

1 Like

Yes but trim() only removes it from the beginning and end of a string. And every blue moon It will miss a '/r' even if its at the end of a string. I've had trim() act funny with some of my returns from the exec node.

lets say I had a string from file like this:


I want:

hello world

As a variable msg.payload with no line break or return in the middle or the end.
This is why i posted the replace option

I didn't mean use a regex replace in a function node, I was saying to use a regex match in the switch node so you don't need the function node at all.

1 Like

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