Operation `users.insertOne()` buffering timed out after 10000ms in, Node.js, MongooseError

Viewed 5962

I am busy creating a project which allows the user to enter "Email" & "Password" to register to the site,

When I try to enter a user using the "Email" & "Password" to enter this site, I get the following error:

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

Unhandled rejection MongooseError: Operation users.findOne() buffering timed out after 10000ms

I have tried all the proposed solutions I found here which have a similar problem, delete the node_modules folder and reinstall mongoose.

Please see my code below, thanks!

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");

const app = express();

app.use(express.static("public"));
app.set('view engine', 'ejs');

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

mongoose.connect = ("mongodb://localhost:27017/userDB", {useNewUrlParser: true, useUnifiedTopology: true});

const userSchema = {
  email: String,
  password: String
};

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

app.get("/", function(req, res){
res.render("home")
});

app.get("/login", function(req, res){
res.render("login")
});

app.get("/register", function(req, res){
res.render("register")
});

app.post("/register", function(req, res){
const newUser = new User({
  email: req.body.username,
  password: req.body.password
  });
  newUser.save(function(err){
    if (err) {
      console.log(err);
    } else {
      res.render("secrets");
   }
 });
});

Can anyone help me out?

Atom images - Project SecretsCode

4 Answers

try adding one more line under mongoose.connect curly braces:-

mongoose.connect("monogodb://localhost....,{useNewUrlParser:true,
    useUnifiedTopology:true,
    useCreateIndex:true}).then(()=>{
console.log(`successfully connected`);
}).catch((e)=>{
console.log(`not connected`);
})

you can check the error in console if not connected by replacing "not connected" with 'e'

I was having the same issue, Make sure that your mongoDB database username and password is alphanumeric with no special symbols. And dont add this:

    useUnifiedTopology:true,
    useCreateIndex:true}) ```
simply write this:
```mongoose.connect(process.env.MONGO_URL,).then(()=>{
    console.log(`successfully connected`);
    }).catch((e)=>{
    console.log(`not connected`);
    }); ```

For a reason my IP address changed, so I had to update it inside Mongo DB - NetworkAccess - Edit - Use the new IP address, maybe you'll see a button to update with current Ip or something like that, click it

try this one :

    const userSchema = new mongoose.Schema({
          email: String,
          password: String
    });
    
    mongoose.model("User", userSchema);
    
    const User = mongoose.model("User");

///////

app.post("/register", function(req, res){
const newUser = new User({
  email: req.body.username,
  password: req.body.password
  });
  newUser.save(function(err){
    if (err) {
      console.log(err);
    } else {
      res.render("secrets");
   }
 });
});
Related