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

fix(collection): handle creating model when connection disconnected with bufferCommands = false #12889

Merged
merged 2 commits into from
Jan 17, 2023

Conversation

vkarpov15
Copy link
Collaborator

Summary

If you create a new model while Mongoose is disconnected, and bufferCommands is false, you'll get the following error:

MongooseError: Cannot call `tests.findOne()` before initial connection is complete if `bufferCommands = false`. Make sure you `await mongoose.connect()` if you have `bufferCommands = false`.

That is unexpected. You should never get that error after calling mongoose.connect(), even with bufferCommands disabled.

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though i am not quite sure what this is solving

i am currently understating it as "creating a model (mongoose.model) after the connection was connected once but is not at the point of model creation while bufferCommands is false"?

@vkarpov15
Copy link
Collaborator Author

Here's a quick overview of when this issue happens:

  1. mongoose.connect() succeeds, bufferCommands is set to false
  2. Mongoose loses connectivity to the database. Perhaps the MongoDB server went down, or there's a network outage.
  3. Create a new model using mongoose.model()
  4. Run await Model.findOne(), get error about initial connection

Copy link
Collaborator

@IslandRhythms IslandRhythms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 merged commit 8b99ca7 into master Jan 17, 2023
@vkarpov15 vkarpov15 added this to the 6.8.4 milestone Jan 17, 2023
@hasezoey hasezoey deleted the vkarpov15/fix-model-while-disconnected branch January 17, 2023 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants