skip to Main Content

I’m new to MERN. I faced an issue while using MongoDB with Mongoose. Genuinely, I have no idea about it. An error like this:

const err = new MongooseError(message);

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

Here is my code:

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/fruitsDB', {useNewUrlParser: true});

const fruitSchema = new mongoose.Schema({
    name: String,
    rating: Number,
    review: String
});

const Fruit = mongoose.model('Fruit', fruitSchema);

const fruit = new Fruit({
    name: "Banana",
    rating: 7,
    review: "Much loved."
});

fruit.save();

Full form of error in terminal:

C:Bekhruz's  Coding JourneyBekhruz's ProgressMongooseAppnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:151
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:Bekhruz's  Coding JourneyBekhruz's ProgressMongooseAppnode_modulesmongooselibdriversnode-mongodb-nativecollection.js:151:23)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)

Node.js v17.7.2

Thanks in advance.

3

Answers


  1. Try adding await at mongoose.connect() function. And handle errors with catch function as shown in this link.

    Login or Signup to reply.
  2. Make a top level async function to wait for the MongoDB to connect

    Try/Catch your save() function.

    (async () => {
      // put your new code here
    
      await mongoose.connect(/* URL */, { useNewUrlParser: true });
    
      //...
      
      try {
        fruit.save();
      } catch (err) {
        console.log('Couldn't save fruit!');
      }
    });
    
    Login or Signup to reply.
  3. kindly check if you are connected with your DB.

    mongoose.connect(
      process.env.MONGO_URL,
      options
    )
    .then(()=>console.log('connected'))
    .catch(e=>console.log(e));
    

    This shall throw a proper error that can be used for debugging.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search