使用 mongoose.createConnection() 与 mongoose.connect() 时查询挂起 [英] Queries hang when using mongoose.createConnection() vs mongoose.connect()

查看:30
本文介绍了使用 mongoose.createConnection() 与 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 = db.model(...) 而不是 User = mongoose.model(...).

This means that instead of User = mongoose.model(...) you need User = db.model(...).

示例(一个两个) 表明这并不复杂,但变化足够微妙以至于 很多人想念它.文档对此也不是很清楚,这可能是根本原因.

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) 你的 kraken 应用程序 &.createConnection

如果您正在构建 kraken 示例之一,则需要进行一些更改.

If you are building on one of the kraken examples, you'll need to make several changes.

  1. 更改 .createConnection 周围的代码,以便您可以访问返回的对象.在当前形式中,您返回的是一个带有 config 函数的对象,但您没有返回 .createConnection 生成的连接对象.
  2. 如果您更改了在 db.config 中配置/创建连接的方式,请更新 index.js.您也许可以避免这种情况,但我怀疑您会围绕新调用重写整个 db.js.
  3. 确保使用模型的代码/控制器可以访问您的 .createConnection 返回的对象.这意味着既可以访问对象,也可以更改设置变量的任何位置,因此它使用格式 var xyz = db.model('XYZ')
  1. Change the code around .createConnection so you can access the object that is returned. In the current form, you are returning an object with a config function but you don't return the connection object that .createConnection generates.
  2. Update index.js if you change the way you configure/create the connection in db.config. You might be able to avoid this, but I suspect you'll rewrite the entire db.js around the new call.
  3. 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 format var xyz = db.model('XYZ'), etc.

抱歉,没有一个简单的单行答案...

Sorry that there isn't a simple one-line answer...

这篇关于使用 mongoose.createConnection() 与 mongoose.connect() 时查询挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆