使用mongoose.createConnection()vs mongoose.connect()时查询挂起 [英] Queries hang when using mongoose.createConnection() vs mongoose.connect()
问题描述
作品:
var mongoose = require('mongoose');
var db = function() {
return {
config: function(conf) {
mongoose.connect('mongodb://' + conf.host + '/' + conf.database);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('db connection open');
});
}
};
};
module.exports = db();
无效:
var mongoose = require('mongoose');
var db = function() {
return {
config: function(conf) {
var db = mongoose.createConnection('mongodb://' + conf.host + '/' + conf.database);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('db connection open');
});
}
};
};
module.exports = db();
插入代码:
'use strict';
var mongoose = require('mongoose'),
User = mongoose.model('User'),
p = require('../lib/promise');
...
app.post('/user', function (req, res) {
res.format({
json: function () {
//extract the user from the req
try {
var user = new User();
user.firstName = req.body.firstName;
user.lastName = req.body.lastName;
user.userName = req.body.userName;
user.password = req.body.password;
user.email = req.body.email;
user.save(function(err, data) {
//omitted
...
推荐答案
不幸的是,这不是一个简单的重构。
Unfortunately, this isn't a simple refactor.
1) .createConnection
vs .connect
1) .createConnection
vs .connect
当使用 .createConnection
时,您可以通过此调用创建的显式连接来访问模型。
When using .createConnection
, you access models via the explicit connection you create with this call.
这意味着,而不是 User = mongoose.model(...)
您需要 User = db.model(...)
。
This means that instead of User = mongoose.model(...)
you need User = db.model(...)
.
示例( one ,两个,三个,四)显示这个isn不是很复杂,但是这种变化是微不足道的,许多人错过了。文件也不是很清楚,这可能是根本原因。
Examples (one, two, three, four) show this isn't complicated but the change is subtle enough that many people miss it. The docs aren't super clear on this either, which is probably the root cause.
2)你的克拉肯应用程序 .createConnection
2) your kraken app & .createConnection
如果你正在建立一个克拉克的例子,你需要进行几个更改。
If you are building on one of the kraken examples, you'll need to make several changes.
- 更改围绕
.createConnection
的代码,以便您可以访问返回的对象。在当前窗体中,您将返回一个包含config
函数的对象,但不返回.createConnection
生成。 - 如果您更改在
中配置/创建连接的方式,请更新
。您可能可以避免这种情况,但我怀疑您将重写整个新的通话中的index.js
db.configdb.js
。 - Make确保使用模型的代码/控制器可以访问您的
.createConnection
返回的对象。这意味着访问对象和更改任何位置的方法可以设置变量,以便使用格式var xyz = db.model('XYZ')
等。 li>
- Change the code around
.createConnection
so you can access the object that is returned. In the current form, you are returning an object with aconfig
function but you don't return the connection object that.createConnection
generates. - Update
index.js
if you change the way you configure/create the connection indb.config
. You might be able to avoid this, but I suspect you'll rewrite the entiredb.js
around the new call. - Make sure code/controllers working with models have access to the object your
.createConnection
returned. This means both a way to access the object and changing anyplace you set a variable so it uses the formatvar xyz = db.model('XYZ')
, etc.
对不起,没有一个简单的单行答案...
Sorry that there isn't a simple one-line answer...
这篇关于使用mongoose.createConnection()vs mongoose.connect()时查询挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!