-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
openUri doesn't flush buffer commands on open (and why createConnection returns a Promise?) #5404
Comments
I have faced the issue when a query on a collection is executed before mongoose connects. The code below never executes the const mongoose = require('mongoose')
mongoose.Promise = Promise
const schema = mongoose.Schema({}, {strict: false});
const Model = mongoose.model('mymodel', schema);
Model.find().exec().then(console.log)
mongoose.connect('mongodb://localhost:27017/testingBug', { useMongoClient: true })
.then(() => {
console.log('connected')
})
.catch(console.error) Here's what happens:
As @ValYouW suggests, calling @ValYouW I suggest to change the title to something like UPDATE: mongoose.connect('mongodb://localhost:27017/testingBug', { useMongoClient: true }, function (ignore, connection) {
connection.onOpen()
}).then(() => {
console.log('connected')
})
.catch(console.error) |
this seems like a legit enhancement to me, i'll add it to the docket. Thoughts @vkarpov15 ? |
You're right about the One of the changes for |
@vkarpov15 Thank you. Returning Promise from createConnection/connect doesn't give users the option to use the "command buffering" feature of mongoose (which is cool) on app startup (unless it is a single-connection and all operations are done on the default connection). |
Yeah I can see that being frustrating. I suppose we can decorate connections with |
@vkarpov15 thanks! |
@ValYouW I agree! I have the exact same issue. 3 different databases, so 3 different |
Fixes, will be released with 4.11.2 |
@vkarpov15 can you please give an example how this solution helps? Because I can't still define a model for this connection since it is a promise and not a connection object. |
@tomgrossman you should be able to in 4.11.4. Is this not the case? |
@vkarpov15 seems to work, thanks! :) |
mongoose 4.11.0
When using
openUri
the connectiononOpen
is not getting called and as a result all collections are in "buffer" mode, maybe the following line should be replaced to_this.onOpen()
https://github.com/Automattic/mongoose/blob/master/lib/connection.js#L739
[Edit] I'll elaborate as I forgot to mention I did a little hack :)
What I was trying to achieve is make
createConnection
to always return the Connection object, whether it was called withuseMongoClient
or not. The fact that it returns a Promise is breaking my code as now I need to wait for the Promise to resolve before I can define models on this connection.So maybe the correct question is "Why createConnection doesn't return the connection but a Promise?", since connection has a buffer mechanism, and it is an emitter that emits an "open" event it make sense to get it immediately (like it was before 4.11.0).
Thoughts?
The text was updated successfully, but these errors were encountered: