mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

how to check if mongoose is already connected/connecting to the db?

Open jonathanong opened this issue 12 years ago • 11 comments

jonathanong avatar Sep 04 '14 22:09 jonathanong

You can either set a callback:

mongoose.connect('uri', function(err) {
  // If no error, successfully connected
});

There are also events:

mongoose.connection.on('connected', function(){});
mongoose.connection.on('error', function(){});
mongoose.connection.on('disconnected', function(){});

vkarpov15 avatar Sep 08 '14 17:09 vkarpov15

+1 @vkarpov15

datatypevoid avatar Aug 03 '15 00:08 datatypevoid

var db = Mongoose.connect('mongodb://localhost/dbname', function(error){
    if(error) console.log(error);

        console.log("connection successful");
});

Both work done at once.

nickborti avatar Aug 09 '16 12:08 nickborti

@nickborti can you clarify your comment? Is the code you specified not working?

vkarpov15 avatar Aug 14 '16 01:08 vkarpov15

thanks a lot @nickborti

Mohammedosman12 avatar Apr 11 '17 04:04 Mohammedosman12

@vkarpov15 Why not? Working just fine.

nickborti avatar Sep 20 '17 16:09 nickborti

or just check the readyState property - mongoose.connection.readyState == 0; // not connected mongoose.connection.readyState == 1; // connected

simonbor avatar Oct 22 '17 21:10 simonbor

So, how to check readyState for connections created using mongoose.createConnection()

kiran-varghese avatar Jul 02 '18 06:07 kiran-varghese

@corescript connections returned from createConnection() have a readyState property too. Here's an example ( using mongoose 5.1.7 )

corescript.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');

function getConnString(n) {
  return `mongodb://localhost/test${String(n)}`;
}

let thenables = [];

for (let i = 0; i < 10; i++) {
  thenables.push(mongoose.createConnection(getConnString(i)));
}

function getConnStates() {
  return mongoose.connections.map((conn) => {
    return conn.readyState;
  });
}

function closeEmAll() {
  return Promise.all(mongoose.connections.map((conn) => conn.close()));
}

async function run() {
  await Promise.all(thenables);
  console.log('`createConnection` returns a connection with a ready state');
  console.log(`The 1st one has state: ${thenables[0].readyState}`);
  console.log('Use the mongoose.connections array and `.map()` to see them all.');
  console.log('one:', getConnStates());
  console.log('mongoose.connections[0] is disconnected, what gives?');
  console.log('0 is reserved for the default connection created by`.connect()`');
  await mongoose.connect('mongodb://localhost/test10');
  console.log('two:', getConnStates());
  console.log('there. that\'s better.');
  return closeEmAll();
}

run();

Output:

issues: ./corescript.js
`createConnection` returns a connection with a ready state
The 1st one has state: 1
Use the mongoose.connections array and `.map()` to see them all.
one: [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
mongoose.connections[0] is disconnected, what gives?
0 is reserved for the default connection created by`.connect()`
two: [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
there. that's better.
issues:

ghost avatar Jul 02 '18 11:07 ghost

(node:1859) UnhandledPromiseRejectionWarning: MongoError: Server at localhost:27 017 reports wire version 0, but this version of Node.js Driver requires at least 2 (MongoDB2.6).

this error is headache for me i am searching a solution from 3 days whenever i try to run an application using codenvy it gives me that error in terminal. please help

vickyhere avatar Nov 30 '18 18:11 vickyhere

@vickyhere sounds like you're running a very old version of the MongoDB server. Connect to localhost:27017 with the mongo shell and double check what version you're running:

$ mongo --host localhost
MongoDB shell version v3.6.2
connecting to: mongodb://localhost:27017/
MongoDB server version: 3.6.6

vkarpov15 avatar Dec 07 '18 18:12 vkarpov15