I have a tcp request node that returns a string containing a series of variables-value pairs:
"!A00=0.25$!A04=228.00$!A22=2.00$"
Each variable-value pair starts with ! and ends with $
In the example, the server returned 3 variables with 3 values
I am looking to get out all values and store them in local flow-variable. (or perhaps an MQTT message)
I think it should be possible with a function node by using 'indexOf' or 'Substring' or 'search' or 'slice' on the string. But I either get the "is not a function" error or I only get the length of the string.
Most of what I found online tells me to first stringify the msg.payload , but I think mine already is a string.
var outp = msg.payload.slice(11,15) ;
msg.payload = outp;
Gives error : msg.payload.slice is not a function"
So I force msg.payload to be a String
var outp = String(msg.payload).slice(11,15) ;
msg.payload = outp;
but it gives me an empty string ""
Why does '.IndexOf' work but '.slice' does not ?
I guess the more basic question is, is there a specific resource for writing Javascript in Node red?
I am completely new to Javascript.
is like declaring the variable output as an object ?
The syntax of the operations is still unclear to me. Do all these operations apply front to back?
Could those have been written as single operations separately?
It seems that the semi-colon is not very strictly observed ( I accidentally left out the one after parts.split("=") and it doesn't complain. l can't omit one like that in C++)
It also seems I didn't "declare" the variable 'parts' specifically anywhere?
(so there are some dots under the first time its used saying it is implicitly 'any' type)
So do I now write another function node to assign all those numbers to flow variables for use elsewhere or at another time?
Ok, I will google a bit first because I think there are more examples dealing with objects than with my specific formatting of the string.
Like I said, the javascript syntax is confusing to me.
Thanks again.
Its so finicky.
I replaced your single line function (your first response) to the separate lines answer.
It says
msg.payload = msg.payload.split("!$");
And it will only get the first variable out.
I replaced it with
msg.payload = msg.payload.split("$!");
So just changed the order of the '$' and '!' in the split parameters
And it spits out the entire object with all name-value pairs.
There are a lot of search results on google for msg.payload. split, but I can't find an actual reference for the function.
So I just add that for my future reference or for the benefit of anyone else looking.
Hahaha, yes, I figured it out.
(I kinda compare split to strtok (in C++) that I am somewhat familiar with, so I keep running into differences between the languages)
And thanks for the link to the resource.