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

**URL:** <https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 May 2023 07:14 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594 "2023-05-22T07:14:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![haph-vi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haph-vi/32/49126_2.png) [@haph-vi](https://discourse.nodered.org/u/haph-vi)\
**Post date:** [22 May 2023 07:14 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594/1 "2023-05-22T07:14:04Z")

</div>

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:

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

```auto
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);
            }
        });
    }
);

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 May 2023 08:37 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594/2 "2023-05-22T08:37:29Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![haph-vi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haph-vi/32/49126_2.png) [@haph-vi](https://discourse.nodered.org/u/haph-vi)\
**Post date:** [22 May 2023 08:50 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594/3 "2023-05-22T08:50:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 May 2023 08:53 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594/4 "2023-05-22T08:53:55Z")

</div>

> [@haph-vi](#):
>
> Were can I find these additional lines for future errors?

> <https://github.com/node-red/node-red/blob/67dd7e30faf1a6fca07f5b745de75f4ad3036ce4/packages/node_modules/%40node-red/nodes/core/function/10-function.js#L109>

> [@haph-vi](#):
>
> 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.

---

<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:** [21 July 2023 08:54 UTC](https://discourse.nodered.org/t/how-to-read-the-stack-trace-of-an-uncaught-exception-in-a-function-node/78594/5 "2023-07-21T08:54:55Z")

</div>

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