Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No error being thrown for undefined variable in node.js with express

I am running node.js with express. I wrote a node module with methods in it so when you go to http://bla.com/module_name/method_name it will run the method.

The method follows the typical style of

exports.method_name(req, res, next);

my main app does something like this:

app.all("*", resSetup, controller, render);

and controller is the thing that will call the method based on the path.

it seems that if there is an undefined variable error in the method, express will just hang there and not throw any error. Nothing will appear in the console log either. I can put a console message right before and after where the error occurs and the before will appear in the log, and after will not.

I can wrap it in a try/catch and get this:

[ReferenceError: blabla is not defined]

but no line numbers or anything.

My guess is that express is somehow preventing the errors from coming up. When I put the error in the function called "controller" that is directly in the route, it shows that error correctly.

It might not matter too much, but here is the code I am working on:

https://github.com/RobKohr/quick-site/blob/master/index.js

Line 189 is where the method call happens.

like image 397
RobKohr Avatar asked Jan 18 '14 15:01

RobKohr


People also ask

How do you check if a variable is undefined in node JS?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.


1 Answers

Building on Ruairi's comment above, I had this same issue with when using 'q' (https://github.com/kriskowal/q) and promises with express - node would hang and no error was generated.

By adding a catch to the end of the promise 'callback' chain I was able to see the error and print it to console etc.

The code ends up looking like:

export function index(req, res) {

    //Create the 'promise'
    var request = req.body;
    var queryJobID = req.query.jobId;
    console.log('queryJobID: ' + queryJobID);
    var jobStatusPromsie = jobManager.job.getStatus(queryJobID);

    Q.all([jobStatusPromsie])
    .then(
        function (result) {
            var responseData = {};
            console.log('Job Status Response received');
            if (result != null) {
                //Without the .catch below an error here will be 'silent'
                console.log('jobStatus result: ' + util.inspect(result, false, null));
                responseData['status'] = 'OK';
                responseData['progress'] = result.progress;
                res.json(responseData);
            } else {
                console.log('jobStatus Error');
                responseData['status'] = 'Error';
            }
            res.json(responseData);
            console.log('jobStatus Response data sent');
        },
        function (error) {
            console.log('Error while getting job status:', error);
            res.json("Error");
        })
    .catch(function(err) {
        //handle errors
        console.log('Promise error while getting job status:', err);
    }); 
}
like image 155
Mick Avatar answered Sep 27 '22 22:09

Mick