2

I have created a mongodb native connection and saved it and then using findOne to query a document.

const Promise = require("bluebird");
const MongoClient = require('mongodb').MongoClient;
let mongoDB = undefined;

const getCollection = (collName) => {
    if (mongoDB) {
        return Promise.resolve(mongoDB.collection(collName));
    } else {
        return MongoClient.connect(EXT_CONFS.MONGODB_URL)
            .then(db => {
                mongoDB = db;
                return Promise.resolve(mongoDB.collection(collName));
            }).catch(e => {
               console.error('Error in MongoDb connection');
            });
    }
};

const findOne = (collName, filter, options) => {
    return getCollection(collName)
        .then(collection => {
            return collection.findOne(filter, options);
        })
        .then(doc => {
            return Promise.resolve(doc);
        }).catch(e => {
            console.error(e);
            return Promise.reject(e);
        });
};

Now this all works fine, but if Mongo ShutsDown / Fails after db client is cached, There is no way to handle error. Error never goes to any catch handler :

console.error('Error in MongoDb connection');

or

console.error(e);

I even tried events :

 mongoDB.on('connecting', function () {
     console.log('connecting');
 });
 mongoDB.on('timeout', function (error) {
     console.log('timeout!');
 });
 mongoDB.on('close', function (error) {
     console.log('close!');
 });
 mongoDB.on('error', function (error) {
     console.error('Error in MongoDb connection: ' + error);
 });
 mongoDB.on('connected', function () {
     console.log('connected!');
 });
 mongoDB.on('connection', function () {
     console.log('connected!');
 });
 mongoDB.on('connect', function () {
     console.log('connected!');
 });
 mongoDB.once('open', function () {
     console.log('connection open');
 });
 mongoDB.on('reconnected', function () {
     console.log('reconnected');
 });
 mongoDB.on('disconnected', function () {
     console.log('disconnected');
 });

but no success still. Using NodeJS 4.5.0, MongoDB-Native driver 2.2.24

vashishatashu
  • 7,720
  • 7
  • 29
  • 34

2 Answers2

0

You should do something like console.error('Failed to connect to mongodb ',e); you are not outputting the error.

Also some events provide an additional parameter and you are outputting those either. In case of failing to connect to an mongodb server, your application should just notify you it's not the best approach to handle mongodb server start/restart from your application use daemons such as systemd or other process monitoring.

Some events are there to just notify the application that connection was lost or an reconnection is attempted, its up to you to handle what is going to be done when those events are emitted.

You can for example attempt to check mongodb status when an disconnect event is emitted an recreate connection object.

Gntem
  • 6,949
  • 2
  • 35
  • 48
  • I am doing console.error and have even tried with events to capture disconnect, but it does not work, i never get even one console. Which events can handle, please provide a working sample if possible. – vashishatashu Apr 28 '17 at 12:18
  • @vashishatashu were exactly in your code you add the event listeners I believe you add the listeners before the mongoDB is assigned the DB connection, add listeners after `mongoDB=db` line it should output events – Gntem Apr 28 '17 at 12:27
  • @Mr.Pheonix : did the same but nothing happened. Could get debugger till : return collection.findOne(filter, options); then the call never came back or on events. – vashishatashu Apr 28 '17 at 13:29
-1

You could wrap the connect statement in a try-catch block.

Manuel Hernandez
  • 504
  • 10
  • 19