33

I am trying to connect my node app to mongodb via mongoose. It seems to be working, as I can add documents, but I get the error { [Error: Trying to open unclosed connection.] state: 2 }.

I created a very simple app, just to make sure everything is working properly before connecting my actual app.

Here is my simple app:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var timeSchema = new Schema({ timestamp: String });
var Time = mongoose.model('Time', timeSchema);

mongoose.connect('mongodb://localhost/mydb');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', function () {

  var testA = new Test({ timestamp: Date() });

});

I also tried adding db.close() to the end, but it made no difference.

This is running on a Ubuntu 14.04 VPS with:

  • Node.js v0.10.3
  • MongoDB 2.6.3
  • Mongoose 1.4.21
Stennie
  • 63,885
  • 14
  • 149
  • 175
hal
  • 4,845
  • 7
  • 34
  • 57

5 Answers5

61

In my opinion, you are trying to create another connection without closing the current one. So, you might want to use:

createConnection() instead of connect().

In your case, it would look like this:

db = mongoose.createConnection('mongodb://localhost/mydb');
lvarayut
  • 13,963
  • 17
  • 63
  • 87
  • 2
    Thanks, that did it. The real issue was indeed that I was trying to create another connection. I had a second script that I didn't think was being called. – hal Aug 11 '14 at 22:20
  • 2
    **Important:** Make sure to construct models from your connection, not from the 'global' mongoose variable directly, or nothing will get saved. In this example you need to create models with `db.model()` instead of `mongoose.model`. See http://stackoverflow.com/a/10200999/3714913 for a good explanation. – Nateowami Aug 22 '16 at 12:44
6

I had the same issue and found that I had the below connection in another file, which was the reason why I couldn't connect with a different database name. The below createConnection is needed:

db = mongoose.createConnection('mongodb://localhost/mydb');

What I had in another file:

db = mongoose.Connection('mongodb://localhost/mydb');
spenibus
  • 4,339
  • 11
  • 26
  • 35
Prolasis
  • 1,323
  • 2
  • 11
  • 8
2

just use mongoose.connect('...'); once.

maybe in your root app.js or index.js file, not in every model or database related files if your are importing (including) them.

Anyways, if you still have doubt you can check it by:

var mongoose = require('mongoose'); 
var db = mongoose.connection;

db.once('connected', function() {
  console.log('mongoDB is connected');
});
Mehdi Raash
  • 8,721
  • 2
  • 29
  • 42
0

shouldn't your

db.once('open', function () {

  var testA = new Test({ timestamp: Date() });

});

be

db.once('open', function () {

  var testA = new Time({ timestamp: Date() });

});

If "Test" is a different schema based on a different connection, that might affect i think

MartianMartian
  • 1,753
  • 1
  • 18
  • 26
0

I had the same issue, but it was due to a typo:

express-sessions instead of express-session

Buddy
  • 10,874
  • 5
  • 41
  • 58