how to check if mongoose is already connected/connecting to the db?
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(){});
+1 @vkarpov15
var db = Mongoose.connect('mongodb://localhost/dbname', function(error){
if(error) console.log(error);
console.log("connection successful");
});
Both work done at once.
@nickborti can you clarify your comment? Is the code you specified not working?
thanks a lot @nickborti
@vkarpov15 Why not? Working just fine.
or just check the readyState property - mongoose.connection.readyState == 0; // not connected mongoose.connection.readyState == 1; // connected
So, how to check readyState for connections created using mongoose.createConnection()
@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:
(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 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