Error registering user MongooseError: Operation `users.findOne()` buffering timed out after 10000ms at Timeout

I’m new to MERN stack. while I was trying to do a simple register endpoint, I’m getting the below error in the command prompt, can someone try to help me out of this please, Thanks in advance

Screenshot of my terminal

index.js

const express = require("express");
const bodyParser = require("body-parser");
const crypto = require("crypto");
const nodemailer = require("nodemailer");

const app = express();
const port = 8000;
const cors = require("cors");
app.use(cors());

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const jwt = require("jsonwebtoken");

const mongoose = require("mongoose");
mongoose
  .connect("mongodb://192.168.29.166/ecommerce-project", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    console.log("connected to mongodb");
  })
  .catch((err) => {
    console.log("error connecting to mongodb", err);
  });

app.listen(port, "192.168.29.166", () => {
  console.log("server is running on port 8000");
});

const User = require("./models/user");
const Order = require("./models/order");

//function to send verification email to the user

const sendVerificationEmail = async (email, verificationToken) => {
  // create a nodemailer trasnsport

  const trasnsporter = nodemailer.createTransport({
    //configure the email service
    service: "gmail",
    auth: {
      user: "test@gmaill.com",
      pass: "ktyhihvqrwmqcpex",
    },
  });

  //compose the email message
  const mailOptions = {
    from: "amazon.com",
    to: email,
    subject: "Email Verification",
    text: `Please click the following link to verify your email : http://localhost:8000/verify/${verificationToken}`,
  };

  // send the email
  try {
    await trasnsporter.sendMail(mailOptions);
  } catch (error) {
    console.log("Error sending verification email", error);
  }
};

//endpoint to register in the app

app.post("/register", async (req, res) => {
  try {
    const { name, email, password } = req.body;

    //Chech if The email is already registered

    const existingUser = await User.findOne({ email });

    if (existingUser) {
      return res.status(400).json({ message: "Email already Registered" });
    }

    //create a new User
    const newUser = new User({ name, email, password });

    //generate and store the verification token
    newUser.verificationToken = crypto.randomBytes(20).toString("hex");

    //save the user to the database
    await newUser.save();

    //send verification email to the user
    sendVerificationEmail(newUser.email, newUser.verificationToken);
  } catch (error) {
    console.log("error registering user", error);
    res.status(500).json({ message: "Registration failed" });
  }
});

//endpoint tp verify the email

app.get("/verify/token", async (req, res) => {
  try {
    const token = req.params.token;

    //FInd the user with the given verification token
    const user = await User.findOne({ verificationToken: token });

    if (!user) {
      return res.status(404).json({ message: "Invalid verification token" });
    }

    //Mark the user as verified
    user, (verified = true);
    user.verificationToken = undefined;

    await user.save();

    res.status(200).json({ message: "Email verified" });
  } catch (error) {
    res.status(500).json({ message: "Email veridication failed" });
  }
});

user.js

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  verified: {
    type: Boolean,
    default: false,
  },
  verificationToken: String,
  addresses: [
    {
      name: String,
      mobileNo: String,
      houseNo: String,
      street: String,
      landmark: String,
      city: String,
      country: String,
      postalCode: String,
    },
  ],
  orders: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Order",
    },
  ],
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

const User = mongoose.model("User", userSchema);

module.exports = User;

package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "backend",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.1",
    "mongoose": "^7.4.3",
    "nodemailer": "^6.9.4",
    "nodemon": "^3.0.1"
  }
}

Hi @Vinothagan_J , welcome to the community.

You are having a problem connecting with your database.

If you are using the Atlas to create a cluster, probably you forgot to configure the network access from your project.

If you are using another server, you need to ensure that it’s able to connect from your machine.

@Jennysson_Junior Thanks for you reply. but as I said I totally new to this.

I’m still not sure, whether I’m connected or not.

I have attached my Screenshots, if that helps, correct If I’m wrong at anything.

Kindly help, Thanks in advance.

Here you can see your connection string in step 3. Look that you must complete the connection string with the password of your user db.

You will use this connection on your code to connect with your cluster. Something like

mongoose
  .connect(YOUR_CONNECTION_STRING, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })

@Jennysson_Junior Thanks for your reply,

As you can see I have added my connection string as like you have mentioned.

Struglging with this connection issue for a week now :frowning:

Have solved the issues please, I’m having the same issue.

@Vinothagan_J Have solved the issues please, I’m having the same issue.

No, I didn’t find the solution either.

I am also having this issue i can’t find a fix it. another here got a solution?

A post was split to a new topic: MongooseError: Operation users.findOne() buffering timed out