MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms

I’m new to MERN stack. I built some MERN app where I didn’t face this type of error. But recently I am making a simple todo app and I face this error when creating a user.
Please help me :cry: . Thanks in advance.
Here is my code:
index.js

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");

const userRoutes = require("./routes/userRoutes");

dotenv.config();

mongoose.connect(
  process.env.MONGO_URL,
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  },
  () => {
    console.log("mongdb is connected");
  }
);

// middleware
app.use(express.json());
app.use(helmet());
app.use(morgan("common"));

//routes
app.use("/app/v1/user", userRoutes);

app.listen(8000, () => {
  console.log("server is up and running");
});

User.js

const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema(
  {
    username: {
      type: String,
      require: true,
      min: 3,
      max: 20,
      unique: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
      min: 6,
    },
    img: {
      type: String,
      required: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("User", UserSchema);

userRoutes.js

const router = require("express").Router();
const User = require("../models/User");

router.get("/register", async (req, res) => {
  try {
    const newUser = new User({
      username: "tahmid",
      email: "tahmid@gmail.com",
      password: "123456",
      img: "https://avatars.dicebear.com/api/human/1.svg",
    });

    const user = await newUser.save();
    res.status(200).json(user);
  } catch (err) {
    console.log(err);
    res.status(500).json(err);
  }
});

module.exports = router;

.env

MONGO_URL = mongodb+srv://myUsername:myPassword@cluster0.lpjsb.mongodb.net/todoDB?retryWrites=true&w=majority

package.json

{
  "name": "todo-api",
  "version": "1.0.0",
  "description": "its a todo rest api",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "author": "tahmid",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^14.3.2",
    "express": "^4.17.2",
    "helmet": "^5.0.2",
    "mongoose": "^6.1.8",
    "morgan": "^1.10.0"
  }
}

Screenshot of my terminal:
image

1 Like

Did you try this? Most of the solutions changed the DNS to google’s DNS, IPv4 8.8.8.8

5 Likes

@Tahmid_Khandokar Some details:

Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.
That’s because mongoose buffers model function calls internally. This buffering is convenient, but also a common source of confusion. Mongoose will not throw any errors by default if you use a model without connecting.

From Mongoose Docs

User.find() is a Model.method() call, and it has a timeout. It is timing out probably because you’re not connecting to the server, but mongoose throws no errors.

You can disable buffering, but it is probably not advisable (it is on by default, so for simple projects it may be the right setting).

But you could at least set a handler for the error event. basically, it is this:

mongoose.connect(
  process.env.MONGO_URL,
  options,
  (err) => {
   if(err) console.log(err) 
   else console.log("mongdb is connected");
  }
);

// or

mongoose.connect(
  process.env.MONGO_URL,
  options
)
.then(()=>console.log('connected'))
.catch(e=>console.log(e));

Or using async/try/catch.

You’ll find out what the error is (I believe).

7 Likes

Thank you. I’ve already change my DNS to google’s public DNS but nothing happened.

Thank you again. Okay, I’ll try to find the error using a handler.

Seriously, I spent a full day for this error!
Thank you so much :green_heart: :green_heart: :green_heart:
Actually the error is in the parsing parameter of connect() method. Maybe in newer version of MongoDB is not supporting useCreateIndex property.

2 Likes

I’m glad it helped, it is quite tricky, and once you get good at MDB i’d completely drop mongoose (just an opinion, MDB driver is really good).

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.