How to read the stack trace of an uncaught exception in a function node

Hello,

I have a function node which throws an error from time to time.
But I can't pin it down where it comes from. This is the stack trace:

22 May 07:53:25 - [error] TypeError: Cannot read property '0' of undefined
at Function node:b8b41c15d84ab98c [Splunk get DemandType]:66:48
..........

What I have read in the node.js stack trace documentation is, that the error occurs on line 66 and column 48.
But the code in the function node doesn't have a column 48 at line 66.

So how can I identify the position of this error?

Thanks!

Here is the function node code:

if(msg.getDemandType == false) {
    return msg;
}

//node.warn(msg);

var splunkjs = global.get("splunkjs");

var service = new splunkjs.Service({username: "username",
                                    password: "password",
                                    scheme:"https",
                                    host:"asdf",
                                    port:"123"});

// Search everything and return the first 10 results
var searchQuery = '| inputlookup sensor_data.csv"';

// Set the search parameters--specify a time range
var searchParams = {
    id: "myjob_"+msg.sourceName,
    earliest_time: "@d",
    latest_time: "now"
};

var msgSplunk=false;

service.search(
    searchQuery,
    searchParams,
    function(err, job) {
        // Display the job's search ID
        //node.warn("Job SID: ", job.sid);
        if (err) {
            node.warn("Error!");
            node.warn(err);
            return;
        }
        
        // Poll the status of the search job
        job.track({period: 200}, {
            done: function(job) {
                //node.warn("Done!");
                
                try {
                    // Get the results and print them
                    job.results({}, function(err, results, job) {
                        //var fields = results.fields;
                        var rows = results.rows;
                        
                        
                        //node.warn(rows[0][0]);
                        msg.Available = rows[0][0] == "Available" ? true : false;
                        msgSplunk = true;
                        node.send(msg);
                    });
                } catch (err) {
                    node.warn(err);
                }
            },
            
            failed: function(job) {
                node.warn("Job failed")
            },
            
            error: function(err) {
                node.warn(err);
            }
        });
    }
);

The Function node wraps your code in a few additional lines, which can mean the error reporting ends up being a bit off.

In this instance, given the context of the error Cannot read property '0' of undefined, the error is most likely this line, as that is the line of code that is attempting to read a property called 0:

msg.Available = rows[0][0] == "Available" ? true : false;

You code is assuming that rows contains at least one element. If it is empty then you will hit the error you are seeing.

OK, thank you!
Were can I find these additional lines for future errors?

That's what I thought too. I wrapped this part in a try_catch block. Why does the error still breaks node-red?

Your try/catch block is around the call to job.results - but the error is being thrown inside its callback function, so will be happening asynchronously.

If you move the try/catch inside the function it will catch the error.

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