question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Buffering timeout even though database is connected

See original GitHub issue

Mongoose 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:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
hasezoeycommented, Sep 30, 2022

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 or npm ls mongoose, if there are multiple entries and they dont say something along the lines of deduped, then it is likely the problem

you can also try to check if the model has been defined before the connecting, with console.log(mongoose.models); or console.log(mongoose.connection.models);(or in your case console.log(db.models) would also work), if it does not show up, then it is likely that the model is defined on a different instance

0reactions
hasezoeycommented, Oct 10, 2022

Hi, I’m also facing the same error. When I print console.log(db.models) it returns an empty object. But the local machine prints all available 3 models. thinking

that 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix "Buffering timed out after 10000ms" Error in ...
This error happens because you're trying to use a model whose connection hasn't connected to MongoDB. Remember that, in Mongoose, ...
Read more >
Operation buffering timed out after 10000ms even DB is ...
insertOne()` buffering timed out after 10000ms whenever I try to register a user. Some solutions say that I didn't connect to DATABASE.
Read more >
MongooseError: Operation `tours.insertOne()` buffering timed ...
My DB connection is successful but whenever I try to save the document this ... insertOne()` buffering timed out after 10000ms at Timeout....
Read more >
Mongoose error : buffering timed out after 10000ms #9732
Hello, i got this error on mongoose with nodejs : version of mongoose : 5.11.8. I'm trying to save data on my mongodb...
Read more >
buffer pool - MySQL connection timeout issues
I am running a PXC environment, and all nodes have the same info as noted above. Developers have reported a lot of MySQL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found