Queries hang when using mongoose.createConnection() vs mongoose.connect()

跟風遠走 提交于 2019-11-26 16:06:02
Matthew Bakaitis

Unfortunately, this isn't a simple refactor.

1) .createConnection vs .connect

When using .createConnection, you access models via the explicit connection you create with this call.

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) your kraken app & .createConnection

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

  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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!