1

I have following code:-

try {            
        user.findOne({ Email: req.body.email }, function (e, d) {
            if (d) {
                res.json({
                    'success': false,
                    'json': null,
                    'message': 'This email already exists!',
                    'status': 200
                });
            } else {
                var u = new user();
                u.Email = req.body.email;
                u.Password = req.body.password;
                u.Name = req.body.name;
                user.save(function (e, d) {
                    res.json(d);
                });
            }
        });
    } catch (ex) {
        console.log(ex.message + " \n" + ex.stack);
        res.json({
            'success': false,
            'json': ex,
            'message': 'Opps! something wen wrong please try again later!',
            'status': 500
        });
    }
}

I have an exception on line user.save(function (e, d) { I solve the issue but the problem is I see catch block doesn't fire at all and node server stop due to exception. if I put try block inside user.findOne catch block will be fire can anyone please explain me why this behavior in node application? Thanks you!

Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
  • [Related question](http://stackoverflow.com/questions/5999373/how-do-i-prevent-node-js-from-crashing-try-catch-doesnt-work?rq=1). – raina77ow Sep 25 '15 at 12:06
  • 2
    No, it's not. Basically, the reason is that `try-catch` scope doesn't cover the callback function (with `user.save` line). You should use `.catch` method (or alternative ways of catching failing promises). – raina77ow Sep 25 '15 at 12:10
  • @raina77ow is right, try catch are not fired if a piece of code placed inside the try is executed asynchronously. Same effect from timeout for example. – Stranded Kid Sep 25 '15 at 12:12
  • 1
    That's one possible way, but that's rarely convenient. Instead I'd recommend promisifying your async code. – raina77ow Sep 25 '15 at 12:14
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch – epascarello Sep 25 '15 at 12:19
  • 1
    @raina77ow should I use this https://www.npmjs.com/package/q library or there is better option available? – Umesh Sehta Sep 25 '15 at 12:20
  • @epascarello https://www.npmjs.com/package/q ? – Umesh Sehta Sep 25 '15 at 12:21

1 Answers1

0

findOne method is asyn process so your try catch statement will not catch the exception which has occurred within Call Back method. You have to put your try catch statement within call back method to catch the exception.

     user.findOne({
 Email: req.body.email
 }, function(e, d) {
 try {
     if (d) {
         res.json({
             'success': false,
             'json': null,
             'message': 'This email already exists!',
             'status': 200
         });
     } else {
         var u = new user();
         u.Email = req.body.email;
         u.Password = req.body.password;
         u.Name = req.body.name;
         user.save(function(e, d) {
             res.json(d);
         });
     }
 } catch (ex) {
     console.log(ex.message + " \n" + ex.stack);
     res.json({
         'success': false,
         'json': ex,
         'message': 'Opps! something wen wrong please try again later!',
         'status': 500
     });
 }

 }););
Bala.Raj
  • 1,011
  • 9
  • 18