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


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?


Here is the function node code:

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


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

var service = new splunkjs.Service({username: "username",
                                    password: "password",

// 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;
    function(err, job) {
        // Display the job's search ID
        //node.warn("Job SID: ", job.sid);
        if (err) {
        // Poll the status of the search job
        job.track({period: 200}, {
            done: function(job) {
                try {
                    // Get the results and print them
                    job.results({}, function(err, results, job) {
                        //var fields = results.fields;
                        var rows = results.rows;
                        msg.Available = rows[0][0] == "Available" ? true : false;
                        msgSplunk = true;
                } catch (err) {
            failed: function(job) {
                node.warn("Job failed")
            error: function(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.