Function node to compare a msg.payload string

I am using the Tesseract node to read some text that I want to compare but it isn't working out.

image

In this case I want to compare the output of the Tesseract node which with the string "DEL"

This is my function node

This is the input and output to the function node
image

Your function seems to work perfectly actually.

The function compares DEL with DEL\n\n and, since the strings are not equals, your function set the msg.payload = 0.

One possible solution could be to remove any new line character in your payload coming from the Tesseract node and then compare it with the "DEL" string. Adding the following line of code before your IF statement should do the job.

payload = payload.replace(/\n/g, "")
1 Like

Or use the trim method
msg.payload = msg.payload.trim();

1 Like

Thanks for the replies, I have tried both methods by adding a line before my IF statement but the result is still 0.

So I have added another function node before this one with a debug node so I can see what is going on.

image

The output from this Function node gives me "DEL" but the IF Function node is still giving me a result of 0
image

image

Add node.warn(msg.payload[0]) to the top of your function - what do you see in the debug sidebar?

1 Like

Thanks, I had just stumbled that by taking out the square brackets then it would check the whole string. So by playing around and for future reference the number in the bracket indicates the character in the string that is going to be compared. Is there any notation in that I can put in that bracket if I wanted to check say the first 3 characters of that string?

Strings are essentially 0 based char arrays (so mystring[1] accesses the 2nd character)

String are objects with many built in functions - 1 of them being substr. Check out the extensive online documentation which also has examples and a dynamic run-able demo where you can test things out & see what happens.

1 Like

Brilliant, thanks for your help

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