node.js 为什么我收到错误“试图打开未关闭的连接。”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25250544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting error "Trying to open unclosed connection."?
提问by Hal Carleton
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 }.
我正在尝试通过猫鼬将我的节点应用程序连接到 mongodb。它似乎有效,因为我可以添加文档,但出现错误{ [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.
我也尝试添加db.close()到最后,但没有任何区别。
This is running on a Ubuntu 14.04 VPS with:
这是在 Ubuntu 14.04 VPS 上运行的:
- Node.js v0.10.3
- MongoDB 2.6.3
- Mongoose 1.4.21
- Node.js v0.10.3
- MongoDB 2.6.3
- 猫鼬 1.4.21
回答by lvarayut
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().
createConnection()而不是connect().
In your case, it would look like this:
在你的情况下,它看起来像这样:
db = mongoose.createConnection('mongodb://localhost/mydb');
回答by Prolasis
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 createConnectionis needed:
我遇到了同样的问题,发现我在另一个文件中有以下连接,这就是我无法连接不同数据库名称的原因。createConnection需要以下内容:
db = mongoose.createConnection('mongodb://localhost/mydb');
What I had in another file:
我在另一个文件中的内容:
db = mongoose.Connection('mongodb://localhost/mydb');
回答by Mehdi Raash
just use mongoose.connect('...');once.
只需使用mongoose.connect('...');一次。
maybe in your root app.jsor index.jsfile, not in every model or database related files if your are importing (including) them.
也许在你的根app.js或index.js文件中,而不是在每个模型或数据库相关文件中,如果你正在导入(包括)它们。
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');
});
回答by King Obi
I had the same issue, but it was due to a typo:
我有同样的问题,但这是由于一个错字:
express-sessionsinstead of express-session
express-sessions代替 express-session
回答by Martian2049
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
如果“测试”是基于不同连接的不同模式,那可能会影响我认为

