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

model buffering rejects model.init() #10706

Closed
kohtala opened this issue Sep 10, 2021 · 0 comments
Closed

model buffering rejects model.init() #10706

kohtala opened this issue Sep 10, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@kohtala
Copy link

kohtala commented Sep 10, 2021

Do you want to request a feature or report a bug?

Bug.

What is the current behavior?

Creating a model before mongoose.connect produces a model where the model.init() returns a promise that is rejected.

The autoCreate and autoIndex are not buffered until the database is connected but instead determined at time of model creation and therefore the options on database have no effect. This problem was there already in mongoose 5.

In mongoose 6 it seems to default to autoCreate and attempt to create the collection without connection.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

console.log({ mongoose: mongoose.version, node: process.version });

const Test = mongoose.model('Test', new mongoose.Schema({ name: String }));

setTimeout(function() {
	mongoose.connect('mongodb://localhost:27017/myapp', { autoCreate: false });
}, 1000);

setTimeout(async function() {
	try {
		await Test.init();
	} catch (e) {
		console.log(e);
		process.exit(1);
	}
}, 2000);

Produces output

$ node bug.js
{ mongoose: '6.0.5', node: 'v14.17.6' }
MongooseError: Connection 0 was disconnected when calling `createCollection`
    at NativeConnection.createCollection (/home/kohtala/js/node_modules/mongoose/lib/connection.js:563:31)
    at /home/kohtala/js/node_modules/mongoose/lib/model.js:1354:13
    at promiseOrCallback (/home/kohtala/js/node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)
    at Mongoose._promiseOrCallback (/home/kohtala/js/node_modules/mongoose/lib/index.js:1151:10)
    at Function.createCollection (/home/kohtala/js/node_modules/mongoose/lib/model.js:1351:23)
    at /home/kohtala/js/node_modules/mongoose/lib/model.js:1269:16
    at /home/kohtala/js/node_modules/mongoose/lib/model.js:1273:5
    at new Promise (<anonymous>)
    at Function.init (/home/kohtala/js/node_modules/mongoose/lib/model.js:1272:16)
    at Mongoose._model (/home/kohtala/js/node_modules/mongoose/lib/index.js:556:9)

What is the expected behavior?

Should buffer the init until there is a connection and keep the Promise unresolved until collection and indices are created, if allowed by the options given during connect.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js v14.17.6, mongoose 6.0.5, mongodb 4.1.1. Server would be 4.4.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 10, 2021
@vkarpov15 vkarpov15 added this to the 6.0.6 milestone Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants