# TypeError: Cannot read property 'replace' of undefined

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-property-replace-of-undefined/14632>\
**Category:** General\
**Created:** [22 August 2019 08:29 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-replace-of-undefined/14632 "2019-08-22T08:29:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stud](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@stud](https://discourse.nodered.org/u/stud)\
**Post date:** [22 August 2019 08:29 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-replace-of-undefined/14632/1 "2019-08-22T08:29:48Z")

</div>

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

Here is the code:

for(i=0; i \< measurements.length; i++){  
dates[measurements[i]] = [parseFloat(msg.payload[measurements[i]].replace( /,/g, '.'))];  
insertInto += dates[measurements[i]];  
if(i \< measurements.length-1){ insertInto += ','}

}  
insertInto += ')';

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 August 2019 08:39 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-replace-of-undefined/14632/2 "2019-08-22T08:39:54Z")

</div>

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.

I'm on my phone but you say you have an array, but your code shows you have nested arrays. arrays in arrays.  
You can also add "event" lines to your code so you can see what is happening. See [https://nodered.org/docs/user-guide/writing-functions#logging-events](https://nodered.org/docs/user-guide/writing-functions#logging-events)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 August 2019 09:04 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-replace-of-undefined/14632/3 "2019-08-22T09:04:53Z")

</div>

> [@stud](#):
>
> msg.payload[measurements[i]].replace

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....

```auto
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.
