I am reading data from a serial port and with test case capturing the required output, then this output is sent to html
now while reading from serial port, there are few unicode charachters in the output. I can remove them by using
.replace(/[^x0000-xFFFF]/g, "").trim();
there are approx 50 places where I need to use this replace and trim method, so I am trying to define a function, where I can pass the output to the function and it will give me clean output. So I do not have to write .replace and .trim to every output.
here is what I tried till now.
This is the function I define to do the replace and trim.
function cleanoutput () {
var output = output.replace(/[^x0000-xFFFF]/g, "").trim();
}
This is the function calling to the output
display.adults = cleanoutput (adults.slice(28, 31));
By doing this I am getting error in function cleanoutput error – “Cannot read property ‘replace’ of undefined”
I am just learning nodejs, need help on making this work.
Advertisement
Answer
There are two problems with your function: first, you are using replace
on a variable that probably is not defined yet but is probably intended to work on a parameter, that is not defined either.
The second problem is that your function is not returning anything, so it is implicitly returning undefined
.
display.adults
will be undefined
after the execution of the cleanoutput
function.
You can fix those problems with something like this:
function cleanoutput (output) {
return output.replace(/[^x0000-xFFFF]/g, "").trim();
}
My suggestion is to dive a little bit deeper into javascript functions.
There are plenty of articles, blog posts, and youtube videos on the topic and I’m sure you will find a good one.