node.jsmongodbmongoskinmonk

DB connection error handling with monk


I am using monk on a code that looks like

var monk = require('monk')
var db = monk('localhost/mydb')

if(!db){
  console.log('no connection')
}

when I run it, console logs 'no connection', but I would like to know why it is not connecting, (maybe see a stack trace' how do I do that?


Solution

  • Looks like this is known bug. https://github.com/Automattic/monk/issues/24

    In your snippet, monk(url) returns db object whether connection or not.

    This is db object on connection

    > dbgood
    { driver:
       { _construct_args: [],
         _native:
          { domain: null,
            _events: {},
            _maxListeners: 10,
            databaseName: 'dbgood',
            serverConfig: [Object],
            options: [Object],
            _applicationClosed: false,
            slaveOk: false,
            bufferMaxEntries: -1,
            native_parser: true,
            bsonLib: [Object],
            bson: [Object],
            bson_deserializer: [Object],
            bson_serializer: [Object],
            _state: 'connected',
            pkFactory: [Object],
            forceServerObjectId: false,
            safe: false,
            notReplied: {},
            isInitializing: true,
            openCalled: true,
            commands: [],
            logger: [Object],
            tag: 1418920835318,
            eventHandlers: [Object],
            serializeFunctions: false,
            raw: false,
            recordQueryStats: false,
            retryMiliSeconds: 1000,
            numberOfRetries: 60,
            readPreference: [Object] },
         _emitter:
          { domain: null,
            _events: {},
            _maxListeners: 50 },
         _state: 2,
         _connect_args: [ 'mongodb://localhost/dbgood', [Object] ] },
      helper:
       { toObjectID: [Function],
         id:
          { [Function: ObjectID]
            index: 10690856,
            createPk: [Function: createPk],
            createFromTime: [Function: createFromTime],
            createFromHexString: [Function: createFromHexString],
            isValid: [Function: isValid] } },
      collections: {},
      options: { safe: true },
      _events: {} }
    

    This is db object when mongodb is not running

    > dbbad
    { driver:
       { _construct_args: [],
         _native: null,
         _emitter:
          { domain: null,
            _events: {},
            _maxListeners: 50 },
         _state: 0,
         _connect_args: [ 'mongodb://dbbad', [Object] ] },
      helper:
       { toObjectID: [Function],
         id:
          { [Function: ObjectID]
            index: 10690856,
            createPk: [Function: createPk],
            createFromTime: [Function: createFromTime],
            createFromHexString: [Function: createFromHexString],
            isValid: [Function: isValid] } },
      collections: {},
      options: { safe: true },
      _events: {} }
    

    Maybe for now, you can use _native._state for checking connection.