4

I'm currently working on error handling when my mongo database is down. I've noticed that when mongo is down, mongoose promises seem to just hang - neither the success or error handler is being called. The following code only prints out calling sign in, and the HTTP request hangs for 2 minutes before timing out on the client side.

router.post('/auth/signin', function (req, res, next) {

    console.log('calling sign in');
    User.find({}).then(() => {
        console.log('success');
    }, (error) => {
        console.log('err', error);
    }).then(() => {
        res.sendStatus(500);
    });

}

I have tried adding the following to my mongoose connection options:

return mongoose.connect(mongoUrl, {
    server: {
        socketOptions: {
            connectTimeoutMS: 1000,
            socketTimeoutMS: 1000
        }
    }
}, done);

But this seems to have had no effect.

Joaquim d'Souza
  • 1,416
  • 2
  • 14
  • 25

0 Answers0