How to Fix "Buffering timed out after 10000ms" Error in Mongoose

Jul 8, 2022

This error happens because you're trying to use a model whose connection hasn't connected to MongoDB. Remember that, in Mongoose, every model has exactly one connection to MongoDB. The buffering timeout is usually due to either registering models on a newly created connection but using mongoose.connect():

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  name: String
});

async function run() {
  // Create a separate connection and register a model on it...
  const conn = mongoose.createConnection();
  conn.model('User', schema);

  // But call `mongoose.connect()`, which connects MongoDB's default
  // connection to MongoDB. `conn` is still disconnected.
  await mongoose.connect('mongodb://localhost:27017');

  await conn.model('User').findOne(); // Error: buffering timed out ...
}

run();

Or by registering models using mongoose.model() but creating a separate connection:

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  name: String
});

async function run() {
  // Create a new connection and connect to MongoDB...
  const conn = await mongoose.
    createConnection('mongodb://localhost:27017/test').
    asPromise();

  // But register a model on Mongoose's default connection
  mongoose.model('User', schema);

  await mongoose.model('User').findOne(); // Error: buffering timed out
}

run();

To fix, make sure you call mongoose.connect() if you're defining models by calling mongoose.model():

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  mongoose.model('User', schema);

  await mongoose.model('User').findOne(); // Works!
}

Want to become your team's MongoDB expert? "Mastering Mongoose" distills 8 years of hard-earned lessons building Mongoose apps at scale into 153 pages. That means you can learn what you need to know to build production-ready full-stack apps with Node.js and MongoDB in a few days. Get your copy!

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Mongoose Tutorials