Buffering timeout even though database is connected
See original GitHub issueMongoose version
6.6.2
Node.js version
16.7.0
MongoDB version
6.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 22.04
Issue
I am working on porting some code from JS to TS, and my database connection has not changed, so I have ruled out networking issues. In fact, the logs say the connection is established, and yet, only after I have converted my models to TS, only then am I experiencing buffer timeouts.
Perhaps I am exporting/importing the mongoose.model
const
incorrectly from my external module? It does know what collection to use, so that seems fine.
For debugging, I am locally using npm pack
in the “models” module, then npm install ../models/models*.tgz
in my “main module”
To minimize the example outside my API layer, I created a population TS script (JS script also returns same error, but I thought TS model imports would work better)
Logs
$ npx ts-node scripts/populatedb.ts mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@mongodb/${MONGODB_DATABASE}?retryWrites=true
Connected to database
Populating database...
FINAL ERR: MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
Closing database connection
Disconnected from database
Code snippet
import {
MongoUserModel as User,
} from '@external-module/models/dist/mongo';
const userCreate = function (email: string) {
const user = new User({ email });
user.save(function (err) {
if (err) {
console.log(err);
return;
}
console.log('New User: ' + user);
});
}
const db = mongoose.connection;
db.on('connected', _ => console.log('Connected to database'));
db.on('disconnecting', _ => console.log('Closing database connection'));
db.on('disconnected', _ => console.log('Disconnected from database'));
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
const userArgs = process.argv.slice(2);
mongoose.connect(userArgs[0]) // have verified this is correct, plus logs say the connection works
.then(() => {
console.log("Populating database...")
userCreate("foo@example.com");
})
External module definition (tsc
output)
import { Schema } from "mongoose";
import { IMongoUser } from ".";
export declare const MongoUserModel: import("mongoose").Model<IMongoUser, {}, {}, {}, Schema<IMongoUser, import("mongoose").Model<IMongoUser, any, any, any, any>, {}, {}, {}, {}, "type", IMongoUser>>;
+ interface definition
import { Types } from "mongoose";
export interface IMongoUser {
_id: Types.ObjectId;
email: string;
}
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top GitHub Comments
i dont see a problem with the model creation or the connection, they are both on the same connection / mongoose instance, the only difference i could think of is that the mongoose package may be duplicated (even same version) and so they are on different instances
this can be checked by
yarn why mongoose
ornpm ls mongoose
, if there are multiple entries and they dont say something along the lines ofdeduped
, then it is likely the problemyou can also try to check if the model has been defined before the connecting, with
console.log(mongoose.models);
orconsole.log(mongoose.connection.models);
(or in your caseconsole.log(db.models)
would also work), if it does not show up, then it is likely that the model is defined on a different instancethat likely means that your models are registered on a different connection, or you are printing before any model could be registered, do you use different connections for your local machine and non-local machine?