# Type Error: Cannot read property of '1' of undefined

**URL:** <https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370>\
**Category:** General\
**Created:** [20 July 2020 03:38 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370 "2020-07-20T03:38:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [20 July 2020 03:38 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/1 "2020-07-20T03:38:19Z")

</div>

Hello, I have a node-red-contrib-file-function which allows me to create a function on nodered outside the nodered itself. My application is simple, I just read data from my influxDB and do some calculations from the data acquired and writes it to influx again (different measurement), however, from time to time (at random interval), I got this error: Cannot read property of '1' undefined.

This is actually my code, on the function "getVal" , that is where my array with index of "1" having an issue.

```auto
> //Don't Edit ====================================================================
var res = msg.payload.results
var numVal = res[0].series[0].values
var i
m_out = []
//================================================================================

// EDIT THIS FOR YOUR CALCULATION ==============================================================
//Measurement is the Tagname
//value: is your calculation formula
//Description is the description of your Tag
for (i=0;i<=numVal.length-1;i++){
    var script = {
        payload: {
            measurement: "CAL3",
            fields:{
                value: Math.sqrt(getval("Counter",i))
            },
            timestamp: getStamp(i)
        }
    }
    m_out.push(script)
}

//==============================================================================================

//DON'T EDIT ==================================================================================

function getval(tagname,j){
    for(var x in res){
        if (res[x].series[0].name == tagname){
            return res[x].series[0].values[j][1] -- > This is where the error is.
        }
    }
}

function getStamp(j){
    if (j == numVal.length -1) {
        global.set("startTime",res[0].series[0].values[j][0])
    }
    return new Date(res[0].series[0].values[j][0]).getTime() * 1000000

}

//================================================================================================

return [m_out]
''

```

Now my question is what could be the possible reason that I am getting this error?  
The node before this node is simply getting the database from influxDB and the results is stored in "res" variable which can be seen in my code above. This is what the the database read looks like:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0bbf2b0c02532ea9c262b3a1e9f690ada73ea521.png)

Hopefully you can help me. THank you.

Regards,  
Henjoe

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 July 2020 05:37 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/2 "2020-07-20T05:37:18Z")

</div>

If it is undefined, it means it can't find it.

Not sure why you would want to load this script externally, as you could put this in a normal function node as well.

Are you sure that you _always_ receive data back from influx ? `time > now()-10s` might be a sensitive spot.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [20 July 2020 06:46 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/3 "2020-07-20T06:46:25Z")

</div>

Hello Bakman, thanksnfor bringing this up, I actually have the same thoughts reqarding my query. May data is updating every seconds, and maybe it has an issue of "timing" since my my query is relative time "Now()-10s".  
I should look on this.

Thanks !

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 July 2020 06:49 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/4 "2020-07-20T06:49:29Z")

</div>

As @bakman2 says, you can run the code in a `function` node.

Doing that you **may** bet a better error message too.

That will help you find out what is going on.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [20 July 2020 07:33 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/5 "2020-07-20T07:33:24Z")

</div>

Thanks for your input !  
There is actually a reason why I used the external function 🙂  
But as the node information says, it is very much the same as core function node, but the code can be written outside (this functionality is what I really need).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 July 2020 07:34 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/6 "2020-07-20T07:34:58Z")

</div>

Yes, but you don't know what is causing the error.

If you indulge doing in Node-Red, it may help you find what is causing the error.

After than you can then make it external again.

---

<div class="post-metadata">

**Author:** ![Henjoe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henjoe/32/52335_2.png) [@Henjoe](https://discourse.nodered.org/u/Henjoe)\
**Post date:** [20 July 2020 07:58 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/7 "2020-07-20T07:58:33Z")

</div>

Oh I see! Now I get it! 🙂 Thanks for clarifying it to me!

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 July 2020 07:59 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/8 "2020-07-20T07:59:31Z")

</div>

Well, it is just a thought.

Node-Red may give you a better error message and so help you find out what is going on.

No promises.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 September 2020 07:59 UTC](https://discourse.nodered.org/t/type-error-cannot-read-property-of-1-of-undefined/30370/9 "2020-09-18T07:59:34Z")

</div>

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