4

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?

Alloys
  • 112
  • 1
  • 8

5 Answers5

6

Per https://github.com/Automattic/monk/pull/142 monk('localhost') can now be used as a promise which resolves when the connection opens and rejects when it throws an error.

i.e.

let db = monk('localhost');

db.catch(function(err) {
  console.log(err)
});
Derek Hill
  • 4,231
  • 1
  • 44
  • 62
2

Hey I have been struggling with this and finally got a very beautiful solution regarding the same. So this a basic node philosophy where the callback has the first argument as the error object. Here in monk this holds good, the code that solves this problem is:

var monk = require('monk');
var db = monk('localhost/mydb', function(err, db){
    if(err){
       console.error("Db is not connected", err.message);
    }
});

As you see a callback function is passed to the monk object itself and that callback has parameters of our interests first being the err object and the next being the db object.

The err object contains all the information about the error that occurs while establishing the connection and corresponding message.

You can go on and look at the err and db object they actually give a deeper insight on how monk has handled things and what object does mongo DB has returned.

Hope this solves your problem.

Farhaan Bukhsh
  • 158
  • 2
  • 7
1

What I found with my experience, is that Monk wont connect if the driver for nodeJS for MongoDB is not in the right version.

I suggest you to double check the drivers version, and in the way you declare the connection string. By any means, resolving the Promise to check the error log as the other suggested here is always a good way to start.

Happy debugging.

0

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.

Kiran Pagar
  • 925
  • 14
  • 21
0

The best solution I found is to add a 'healthcheck' collection to your database, add a document, and try a find command on it. If you get an error from the find command, then log an error.

The problem with other solutions, like checking the db object, is that your status will most likely remain 'connecting' until all your app.js script is finished running through. Monk queues your pre-connection db calls, so making a health check call to the database ensures that your script will wait for the connection sequence to finish before issuing an error or success message.