I'm setting up a login system with nano, passport and couchdb. Things are mostly working but when couchdb is offline I'm given this error:
(node:893) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5984
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
(node:893) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:893) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My code:
const LocalStrategy = require('passport-local').Strategy;
const nano = require('nano')('http://admin:password@localhost:5984');
const users = nano.use('users');
const bcrypt = require('bcrypt');
module.exports = function(passport) {
passport.use(new LocalStrategy(
function(username, password, done) {
users.view('auth', 'auth', {'key': username, 'include_docs': true})
.then(dbresponse => {
if (dbresponse.rows.length === 1) {
const user = dbresponse.rows[0].doc;
bcrypt.compare(password, user.password, function(err, result) {
if(result === true) {
done(null, user)
} else {
done(null, false)
}
});
} else {
done(null, false);
}
})
}
));
};
I can't have the entire app stop working when the database is offline. I'd like to handle this error somehow but I can't figure out how.