skip to Main Content

I’m new in using MERN Stack & I’m trying to connect Mongo and Node but facing this issue while inserting Data into Database, using MongoDb Compass

Index.js

const connectToMongo = require('./db');
const express = require('express')
connectToMongo();

const app = express()
const port = 3000

app.use(express.json())

//Available Routes
app.use('/api/auth', require('./routes/auth'))
app.use('/api/auth', require('./routes/notes'))

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

User.js

const mongoose = require('mongoose');
const { Schema } = mongoose;

const UserSchema = new Schema({
        name:{
            type: String,
            require: true
        },
        email:{
            type:String,
            require:true,
            unique: true
        },
        password:{
            type:String,
            require:true
        },
        timestamp:{
            type:Date,
            default:Date.now
        }
  });

  module.exports = mongoose.model('user', UserSchema)

auth.js

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


router.get('/', (req, res)=>{
        console.log(req.body)
        const user = User(req.body)
        user.save()
        res.send(req.body)
})


module.exports = router

db.js

const mongoose = require('mongoose')
const mongoURI = "mongodb://localhost:27017/"

const connectToMongo=()=>{
    mongoose.set("strictQuery", false);
    mongoose.connect(mongoURI,()=>{
        console.log("Connected to Mongo Successfully")
    })
}

module.exports = connectToMongo;

ThunderClient Request:

{
  "name":"pratik",
  "email":"[email protected]",
  "password":"6626"
}

Error:
const err = new MongooseError(message);
^

MongooseError: Operation users.insertOne() buffering timed out after 10000ms
at Timeout. (D:StudyReactMERNinotebookbackendnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:175:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)

I guess the problem is because of the newer version, I’m trying to read the Docs and StackOverFlow but unable to Solve this Error what should I Do

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Everyone the Solution was quite simple, Just Change your localhost to 127.0.0.1:270


  2. You get the error because you are not connected to mongo actually.
    You miss async/await in db.js

    const connectToMongo = async () => {
    mongoose.set("strictQuery", false);
    await mongoose.connect(mongoURI,
    // you can add this opts
          { keepAlive: true,
            useNewUrlParser: true,
            useUnifiedTopology: true,
        })
    
    console.log("Connected to Mongo Successfully")
    

    }

    And also later when you are calling user.save() it also should be async:

    async (req, res)=>{
        console.log(req.body)
        const user = User(req.body)
        await user.save()
        res.send(req.body)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search