'When trying to sign in - MongooseError: Operation `users.findOne()` buffering timed out after 10000ms

This is my mongoose file :


// require the library
const mongoonse = require('mongoose');

// connect to the database
mongoonse.connect('mongodb://localhost/codeial_development');

// acquire the connection (to check if it is successful)
const db = mongoonse.connection;

// error
db.on('error',console.error.bind(console,'Error connecting to Mongodb'));

// up and running then print the message
db.once('open', function(){
    console.log("Successfully connected to the database")
});

module.exports = db;

This is my index.js file :

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
const port = 8000;
const expressLayouts = require('express-ejs-layouts');
const db = require('./config/mongoose');
// use for session cookie
const session = require('express-session');
const passport = require('passport');
const passportLocal = require('./config/passport-local-strategy');
const MongoStore = require('connect-mongo');

app.use(express.urlencoded());

app.use(cookieParser());

app.use(express.static('./assets'));

app.use(expressLayouts);

// extract styles and scripts from sub-pages into the layout
app.set('layout extractStyles',true);
app.set('layout extractScripts',true);

// set up the view engine
app.set('view engine','ejs');
app.set('views','./views');

// mongo store is used to store the session cookie in the database
app.use(session({
    name: 'codeial',
    // TODO change the secret before deployment in production mode
    secret: 'blah blah',
    saveUninitialized: false,
    resave: false,
    cookie: {
        maxAge: (1000 * 60 * 100)
    },
    store: new MongoStore({
        mongoUrl: "mongodb://localhost/session",
        autoRemove: 'disabled'
    },function(err){
        console.log(err || 'connect-mongodb setup ok');
    })
}));

app.use(passport.initialize());
app.use(passport.session());

app.use(passport.setAuthenticatedUser);

// use express router
app.use('/',require('./routes/index.js')); // Middleware

app.listen(port,function(err){
    if(err)
    {
        console.log('Error: ',err);
        console.log(`Error in running the Server : ${err}`); // interpolation
    }
    else
    {
        console.log(`Server is running on port: ${port}`);
    }
});



Once I run the server it shows connected to the database but as soon as I try to sign-in it throws error! The error I am getting is :(

MongooseError: Operation users.findOne() buffering timed out after 10000ms at Timeout. (/Users/vasuparbhakar/Library/CloudStorage/OneDrive-officeapp.org/CODING/Full Stack Web D/Back End/0_Projects/Codeial/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:149:23) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source