Home > Net >  "message": "errorMongooseError: Operation `userinfos.insertOne()` buffering timed out
"message": "errorMongooseError: Operation `userinfos.insertOne()` buffering timed out

Time:12-29

I'm currently creating a new API with MongoDB and Express, and I'm currently having this issue "message":

"errorMongooseError: Operation userinfos.insertOne() buffering timed out after 10000ms"

his is the way that I setup my API calls:

const express=require('express');
const router=express.Router();
const Userinfo=require("../Models/Userinfo")

router.get('/',(req,res)=>{
    res.send("we are Post")

});



router.get('/Specic', async(req,res)=>{
    try{
        const data=await Userinfo.find();
        console.log(data);
        res.json(data)
    }
    catch(err)
    {
        res.json({message:err})
    }

})
router.post("/",(req,res)=>{
const test= new Userinfo({
    "Fname":req.body.Fname,
    "Lname":req.body.Lname
});
console.log(test);
test.save().then(data=>{
    res.json(data);
}).catch((err)=>{res.json({message:"error" err})})
})
module.exports=router;

MoDel Defining like this Userinfo.js

const mongoose=require('mongoose');
/*const PoistSchema=mongoose.Schema({
    Fname:String,
    Lname:String,
    DOB:Date.now()
});*/

const PostSchema=mongoose.Schema({
    Fname:{
        type:String,
        require:true
    },
    Lname:{
        type:String,
        require:true
    },
    DOB:{
        type:String,
        default:Date.now
    },
});

module.exports=mongoose.model("Userinfo",PostSchema)

App.js

const express=require('express');
const app=express();
const mongoose=require('mongoose');
require("dotenv/config");
const bodyParser=require("body-parser")
///Middlewares
//app.use(auth);
app.use(bodyParser.json());
const postroutes=require("./Routes/Posts");
app.use("/post",postroutes)

app.get('/',(req,res)=>{
    res.send("we are om")

})

app.get('/posts',(req,res)=>{
    res.send("we are Post")

})
try {
     mongoose.connect(process.env.DBConnection,{useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex:true},()=>{
         console.log("Sucess");
     },(error)=>{
         console.log(error);

     });
     console.log("ConnectedZZ")

  } catch (error) {
      console.log(error);
  }
app.listen(3000);

Is there any suggestion to inset and get data.While getting data not getting any error.

CodePudding user response:

There can be several reasons for this issue occurred, but let's start with:

As We can read in docs

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.

So it looks like Your model is being called before the connection is established. To handle this error, you should use .catch() or try/catch with async/await.That should solve your problem.


Here You have example code snippet:

(async () => {
  try {
    await mongoose.connect(process.env.DBConnection)
    console.log('MongoDB connected!!');
  } catch (err) {
    console.log('Failed to connect to MongoDB', err);
  }
})()

CodePudding user response:

you can confirm if the DB is correct before calling the DB calls.

  • Related