Mongo DB buffering time out after 10000ms

```const {Schema, model} = require(‘mongoose’);

const floorSchema = new Schema({
*** floorId: {***
*** type: String,***
*** required: true,***
*** unique: true***
*** },***
*** floorNumber: {***
*** type: Number,***
*** required: true,***
*** unique: true***
*** },***
*** floorName: {***
*** type: String,***
*** required: true,***
*** unique: true***
*** },***
*** description: {***
*** type: String,***
*** required: true***
*** },***
*** imageUrl: {***
*** type: String,***
*** required: true***
*** },***
*** chambers: [{***
*** type: Schema.Types.ObjectId,***
*** ref: ‘Chamber’***
*** }]***
});

module.exports = model(‘Floor’, floorSchema);
strong text```

const Floor = require(‘…/models/FloorsSchemaFC’);
const Chamber = require(‘…/models/ChamberSchemaFC’);

// Create floor object
const floor4 = new Floor({
floorId: ‘floor4’,
floorNumber: 4,
floorName: ‘Ground floor’,
description: ‘Your description’,
imageUrl: ‘Your image URL’,
});

// Save floor to database
floor4.save()
.then(() => {
console.log(‘Floor 4 saved to database’);

// Create chambers
const chambers4 = [
  {
    chamberNumber: 1,
    description: 'Chamber 1 description',
    stats: {
      hp: 100,
      attack: 80,
      speed: 10,
      defense: 20
    },
    image: {
      url: 'https://example.com/chamber1.jpg',
      altText: 'Chamber 1 image'
    },
    floorId: floor4._id
  },
  {
    chamberNumber: 2,
    description: 'Chamber 2 description',
    stats: {
      hp: 200,
      attackSpeed: 20,
      defense: 30
    },
    image: {
      url: 'https://example.com/chamber2.jpg',
      altText: 'Chamber 2 image'
    },
    floorId: floor4._id
  }
];

// Save chambers to database
const chamberPromises4 = chambers4.map(chamberData => {
  const chamber = new Chamber(chamberData);
  return chamber.save();
});

return Promise.all(chamberPromises4);

})
.then(() => {
console.log(‘Chambers of floor 4 saved to database’);
})
.catch(error => {
console.error(‘Error saving floor 4 and chambers:’, error);
throw error;
});

The above piece of code is floor schema. 
The below one is implementation of the floor schema to create a floor. 
when i run this code it gives me the error below 
Error saving floor 2 and chambers: MongooseError: Operation `floors.insertOne()` buffering timed out after 10000ms

   at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

I tried many differnt aprroaches, such whitelisting all the ips with 0.0.0.0/0 changing DNS, shifting to a better network nothing seems to be working m still left with the error can anyone guide me through. Thanks.

Can you start with a simple troubleshooting script? Something like the following:

const mongoose = require('mongoose');

mongoose.connect('your_mongodb_uri', { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log('Connected to MongoDB'))
  .catch(err => console.error('Could not connect to MongoDB', err));

Also - you can play with the timeout settings on your connection like this:

mongoose.connect('your_mongodb_uri', { useNewUrlParser: true, useUnifiedTopology: true, serverSelectionTimeoutMS: 30000, // 30 seconds socketTimeoutMS: 45000, // 45 seconds });

The last thing I would suggest is to incorporate more verbose error messages… console.logs… like this:

mongoose.connection.on('connected', () => {
  console.log('Mongoose connected to MongoDB');
  // Your database operations here
});

mongoose.connection.on('error', err => {
  console.error('Mongoose connection error:', err);
});

Let us know if this helps.

1 Like

What I did was increase the timeout of the connection function and logged the stack trace as u said. The error was caused because of the file floor1.js was outside of the scope of my index.js in the work space. The index file was completing all of its function before getting to save floor1.js and hence timeout. I used node fs and wrapped every floor files in index with increase in timeout and its working fine now.
P.S. Logging really helps.

Thanks for the idea Michael.

1 Like

Great - thanks for closing the loop.