Why am I getting error "Trying to open unclosed connection."?
Asked Answered
P

5

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
Prau answered 11/8, 2014 at 19:3 Comment(0)
N
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');
Nucleolated answered 11/8, 2014 at 19:38 Comment(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.Prau
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 https://mcmap.net/q/452781/-mongoose-js-instance-save-callback-not-firing for a good explanation.Burk
W
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');
Whittemore answered 5/10, 2015 at 2:4 Comment(0)
T
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');
});
Theresatherese answered 1/6, 2017 at 9:49 Comment(0)
L
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

Logorrhea answered 31/12, 2014 at 22:3 Comment(0)
Z
0

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

express-sessions instead of express-session

Zeb answered 28/1, 2017 at 6:17 Comment(1)
@Buddy thanks for the edit, I should have used the snippetZeb

© 2022 - 2024 — McMap. All rights reserved.