Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

60.[半夜改bug] mongoose 的 createConnection 和 connect #65

Open
ccforward opened this issue Apr 3, 2017 · 2 comments
Open

60.[半夜改bug] mongoose 的 createConnection 和 connect #65

ccforward opened this issue Apr 3, 2017 · 2 comments

Comments

@ccforward
Copy link
Owner

[半夜改bug] mongoose 的 createConnection 和 connect

代码出 bug 大半夜的被 mongoose 的连接函数搞惨

因为代码里 mongo 数据库的连接方式的代码被改成了 const db = mongoose.createConnection(mongo.url(), mongoOptions),所以在调用数据插入函数时 Promise 的 resolve 和 reject 都没有返回,也没有打印任何异常信息。

查了业务逻辑、本地 mongo 的读写都没问题,最后发现是 mongo 的连接文件 connect.js 被修改了。

因为用了 mongoose.createConnection 所以此时的 model 应该是 db.mongoose.model('Product')

而之前的是用默认的 mongoose.connect() 方式连接的数据库,获取 model 的代码也应该是 const ProductModel = db.mongoose.model('Product', ProductSchema)

数据库的链接方式被改了,model 没有变,所以增删改查的代码一直被挂起,没有任何的返回。😂😂😂

API和示例代码

Mongoose#connect

API

Opens the default mongoose connection.

Mongoose#connect 开启默认的 mongoose 连接。如果只连接一个库,就用这个默认的。

1.连接数据库 mongo.js

const db = mongoose.connect(mongo.url(), mongoOptions)
exports.mongoose = db
  1. CRUD操作
const db = require('./mongo.js')
const NameSchema = new Schema({
  name: String
})

const NameModel = db.mongoose.model('Name', NameSchema)

class NameDAO = {
  ...
}

Mongoose#createConnection

API里是这样写的

Each connection instance maps to a single database. This method is helpful when mangaging multiple db connections.

Mongoose#createConnection 是适合于多个数据库连接的方式

1.连接数据库 mongo.js

const db = mongoose.createConnection(mongo.url(), mongoOptions)
const Schema = mongoose.Schema
const NameSchema = new Schema({
  name: String
})
db.model('Name', NameSchema)
exports.mongoose = db
  1. CRUD操作
const db = require('./mongo.js')
const NameModel = db.mongoose.model('Name')

class NameDAO = {
  ...
}

最后

要多看 API 文档 。。。。

@ccforward ccforward self-assigned this Apr 3, 2017
@ccforward ccforward changed the title 60.[半夜改bug] mongoose 的 createConnection 和 connect. 60.[半夜改bug] mongoose 的 createConnection 和 connect Apr 4, 2017
@islishude
Copy link

这里需要使用连接的返回值定义Model?文档并没有啊,文档是直接是使用mongoose.model定义的

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useMongoClient: true });
mongoose.Promise = global.Promise;

var Cat = mongoose.model('Cat', { name: String });

var kitty = new Cat({ name: 'Zildjian' });
kitty.save(function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('meow');
  }
});

@wmzy
Copy link

wmzy commented Dec 8, 2017

使用默认链接,为什么不在进程初始化的的时候创建链接,然后:

// 定义schema
const mongoose = require('mongoose')
const NameSchema = new Schema({
  name: String
})
// 如果也在进程初始化的时候加载,此处不用export
mongoose.model('Name', NameSchema);


// curd
const mongoose = require('mongoose')
const NameModel = mongoose.model('Name')

...

还有多个数据库的时候为什么不这么导出:

module.exports = db;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants