-
-
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
Unable to use global models created with connections returned by useDb() #11240
Comments
What is the error message? |
Although the issue is the same (we cannot use Main project error:
On mongoose 5.12.7, the version we were using before 5.13.14, we had Repro scenario error:
We've attempted to use mongoose 6 but on this version, the script hangs and don't get through |
const mongoose = require('mongoose');
const {Schema} = mongoose;
const connect = async () => {
await mongoose.connect('mongodb://localhost:27017/order', { useNewUrlParser: true });
global.db1 = mongoose.connection;
global.db2 = global.db1.useDb('location')
}
async function run() {
await connect();
const OrderSchema = new Schema({
status: String,
})
global.Order = mongoose.model('Order', OrderSchema);
const LocationSchema = new Schema({
name: String,
})
global.Location = global.db2.model('Location', LocationSchema);
await mongoose.connection.dropDatabase();
// Both of these works
const location = await Location.create({name: 'Paris'})
const order = await Order.create({status: 'PAID'})
// We close the connection & reconnect to them
await mongoose.connection.close();
await connect();
console.log(await Order.find({})); // This works & displays results
console.log(await Location.find({})); // This throws MongoError: pool destroyed
console.log('done');
await mongoose.connection.close();
}
run(); happens on 6.1.7 as well |
If I comment out the three lines here Line 791 in 976b137
then it seems to work? |
Do you want to request a feature or report a bug?
What is the current behavior?
We are using global model and connections in our codebase. Some of these models are being created using connections returned by
useDb
. Using those models after closing initial connection & reconnecting to it throw err. Models created withmongoose.connection
works fine though.If the current behavior is a bug, please provide the steps to reproduce.
order.js
location.js
main.js
What is the expected behavior?
Location.find({})
should return results just likeOrder.find({})
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: