Thursday, 8 June 2017

Mongodb Monk reauthentication on connection

I have a node program connecting to a MongoDB. Theres a production server where we do proper authentication to connect to the db, but during development on our local machines it's just more tedious to keep track of authentication, especially because most of the time we completely wipe the db often. So resetting up authentication becomes even more tedious. So my solution was to attempt to connect to the db securely, and then if it fails then try to connect to the db in the dev fashion. Heres the code:

var db = require('monk')('username:password@localhost/TESTR', {authSource:'admin'});

db.catch(function(err) {
    clog.i("MONGO AUTHENTICATION FAILED, USING NO AUTH CLIENT");
    db = require('monk')('localhost/TESTR')
});

The problem is that this doesn't work. The rest of the app just complains about the authentication failure the first attempt had. Is there a better solution to this? Or am I just a moron?



via Ronin

No comments:

Post a Comment