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

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.

> //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:

Hopefully you can help me. THank you.

Regards,
Henjoe

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.

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 !

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.

Thanks for your input !
There is actually a reason why I used the external function :slight_smile:
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).

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.

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

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.

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