Hi,
I have a problem with my function node.
first of all, I have an array and I want to replace the points into commas and parse String into numbers. But it always says that the 'replace' is undefined. Why? can somebody help me pls
if you place your code in </> it makes it easier to read.
Rather than trying to do everything in one line, start off by breaking your code down.
so do the replace on one line.
then the parsefloat it will make it easier to see where the issue is.
as your title says, its undefined.
I can pretty much gaurantee that at some point in your loop msg.payload[measurements[i]] will be undefined;
As @ukmoose said, break it down. e.g. create extra variables and debug them. e.g....
for(i=0; i < measurements.length; i++){
let m = measurements[i];
if(!m){
node.warn(`measurements[${i}] is empty. Accessing elements of msg.payload[ ] will fail `)
}
let item = msg.payload[m];
if(typeof item != "string"){
node.warn(`msg.payload[${m}] is not a string so calling .replace() will cause an error. `)
}
dates[m] = [parseFloat(item.replace( /,/g, '.'))]
insertInto += dates[m];
if(i < measurements.length-1){
insertInto += ','
}
}
insertInto += ')';
I haven't finished the code or tested it but it should give you an idea on how to break it down and debug it.