Mon connection error handling

I am using monk for code that looks like

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

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

when I run it, in the console logs there is "no connection", but I would like to know why it does not connect (maybe see the stack trace ", how to do this?

+4
source share
4 answers

This seems to be a known bug. https://github.com/Automattic/monk/issues/24

In your snippet, monk (url) returns a db object regardless of whether the connection is established.

This is a db object when connected

> 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 a 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: {} }

Perhaps at the moment you can use _native._state to test the connection.

0

Per https://github.com/Automattic/monk/pull/142 monk('localhost') , , , .

.

let db = monk('localhost');

db.catch(function(err) {
  console.log(err)
});
+3

, , , . , node, . , , , , :

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

, monk, , err, - db.

err , .

err db, , Mongo DB.

, .

+1

, , "healthcheck" , find. find, .

The problem with other solutions, such as checking the db object, is that your status is likely to remain “connected” until all of your app.js script applications are complete. Monk queues your db pre-connection calls, so calling the health check service on the database ensures that your script will wait for the connection sequence to complete before issuing an error message or successful completion.

0
source

Source: https://habr.com/ru/post/1568156/


All Articles