{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3856","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3856/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3856/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3856/events","html_url":"https://github.com/Automattic/mongoose/issues/3856","id":131489717,"number":3856,"title":"returning mongoose object not working","user":{"login":"nschloe","id":181628,"avatar_url":"https://avatars.githubusercontent.com/u/181628?v=3","gravatar_id":"","url":"https://api.github.com/users/nschloe","html_url":"https://github.com/nschloe","followers_url":"https://api.github.com/users/nschloe/followers","following_url":"https://api.github.com/users/nschloe/following{/other_user}","gists_url":"https://api.github.com/users/nschloe/gists{/gist_id}","starred_url":"https://api.github.com/users/nschloe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nschloe/subscriptions","organizations_url":"https://api.github.com/users/nschloe/orgs","repos_url":"https://api.github.com/users/nschloe/repos","events_url":"https://api.github.com/users/nschloe/events{/privacy}","received_events_url":"https://api.github.com/users/nschloe/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/90","html_url":"https://github.com/Automattic/mongoose/milestones/4.4.3","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/90/labels","id":1566921,"number":90,"title":"4.4.3","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":17,"closed_issues":0,"state":"open","created_at":"2016-02-05T19:31:50Z","updated_at":"2016-02-05T19:34:33Z","due_on":"2016-02-09T05:00:00Z","closed_at":null},"comments":4,"created_at":"2016-02-04T22:17:37Z","updated_at":"2016-02-05T21:19:48Z","closed_at":null,"body":"Interestingly, when upgrading to 4.4.1 (from 4.3.x), I noticed that returning the mongoose object doesn't work anymore,\r\n```js\r\nimport mongoose from 'mongoose';\r\n\r\nexport const init = co.wrap(function* _init() {\r\n  yield mongoose.connect(\r\n    config.mongodbUri,\r\n    {\r\n      server: {\r\n        socketOptions: {\r\n          keepAlive: 1,\r\n        },\r\n      },\r\n      replset: {\r\n        socketOptions: {\r\n          keepAlive: 1,\r\n        },\r\n      },\r\n    }\r\n  );\r\n\r\n  mongoose.connection.on('error', (err) => {\r\n    process.exit(1);\r\n  });\r\n\r\n  return mongoose;\r\n});\r\n```\r\nalways returns `undefined`. Within this function, it's all fine; `mongoose` is the fat object it used to be. I'm puzzled as to why this may happen."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/180557798","html_url":"https://github.com/Automattic/mongoose/issues/3856#issuecomment-180557798","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3856","id":180557798,"user":{"login":"hgcummings","id":1159423,"avatar_url":"https://avatars.githubusercontent.com/u/1159423?v=3","gravatar_id":"","url":"https://api.github.com/users/hgcummings","html_url":"https://github.com/hgcummings","followers_url":"https://api.github.com/users/hgcummings/followers","following_url":"https://api.github.com/users/hgcummings/following{/other_user}","gists_url":"https://api.github.com/users/hgcummings/gists{/gist_id}","starred_url":"https://api.github.com/users/hgcummings/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hgcummings/subscriptions","organizations_url":"https://api.github.com/users/hgcummings/orgs","repos_url":"https://api.github.com/users/hgcummings/repos","events_url":"https://api.github.com/users/hgcummings/events{/privacy}","received_events_url":"https://api.github.com/users/hgcummings/received_events","type":"User","site_admin":false},"created_at":"2016-02-05T21:19:48Z","updated_at":"2016-02-05T21:19:48Z","body":"This just tripped me up as well, as I wasn't treating mongoose itself as thenable but was returning it in a promise. It's quite confusing behaviour, and not at all obvious what's going on. Here's a minimal repro case that I just created for submitting a new issue (before spotting this one):\r\n\r\n### config.js\r\n```\r\n'use strict';\r\n\r\nlet mongoose = require('mongoose');\r\nmongoose.Promise = Promise;\r\n\r\nlet db = mongoose.connection;\r\nmongoose.connect(process.env.MONGODB_URL);\r\nmodule.exports = new Promise(function(resolve, reject) {\r\n    db.once('open', () => {\r\n        console.log('resolved mongoose', typeof mongoose);\r\n        resolve(mongoose);\r\n    });\r\n    db.on('error', reject);\r\n});\r\n```\r\n\r\n### index.js\r\n```\r\n'use strict';\r\n\r\nconst assert = require('assert'); \r\n\r\nrequire('./config.js').then(mongoose => {\r\n    console.log('received mongoose', mongoose);\r\n    assert.ok(mongoose);\r\n}).then(\r\n    () => process.exit(),\r\n    err => { console.log(err); process.exit(1) }\r\n);\r\n```\r\n\r\nThe config.js module is designed to return a promise that provides a mongoose object only when the database connection is ready. I don't think I'm doing anything particularly unusual here (although genuinely happy to be corrected on this!) but the outcome is very surprising, and doesn't make sense without a deep understanding of how mongoose is implemented. The fact that mongoose is a thenable that resolves to `undefined` is quite a gotcha (especially as it didn't used to work like this in the previous minor version).\r\n\r\nThe other GitHub issues referred to above suggest that `mongoose.connect()` should return a promise-like object, which suggests that the correct fix for me is just to replace my `config.js` with:\r\n```\r\n'use strict';\r\n\r\nlet mongoose = require('mongoose');\r\nmongoose.Promise = Promise;\r\nmodule.exports = mongoose.connect(process.env.MONGODB_URL); // Promise-like\r\n```\r\n\r\nHowever, this doesn't work either, and also exports a promise that resolves to undefined. Is this expected?\r\n\r\nFor now, I'm just returning another object containing the mongoose object (as discussed above). Could you clarify what will be the 'correct' approach long term? - Would the next major version expect me to treat `mongoose.connect()` as thenable, allow me to return the mongoose object in a promise, or be compatible with both approaches?"}}