Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Mongoose error suddenly due to MongooseError: Buffering timeout #14388

Closed
1 task done
Sigmaxii opened this issue Feb 27, 2024 · 19 comments
Closed
1 task done

New Mongoose error suddenly due to MongooseError: Buffering timeout #14388

Sigmaxii opened this issue Feb 27, 2024 · 19 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity Stale

Comments

@Sigmaxii
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.2

Node.js version

18.x

MongoDB version

5.x

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Issue

Error

MongooseError: Operation `autodeletes.findOne()` buffering timed out after 10000ms at Timeout.<anonymous> (C:\Users\alban\documents\github\autodelete-v14\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:186:23) at listOnTimeout (node:internal/timers:573:17) at process.processTimers (node:internal/timers:514:7)

What I've done after research :

  • Restart my vps :clown:
  • Give access to my IP separately than just have the 0.0.0.0
  • Try to connect via compass and check if there is any issues (no issues)
  • Try to find different ways to connect but nothing seemed to do anything.

Right now, I am using mongoose on node.js , I havent touched the code for a while and this cam up yesterday, as a result my app will not work.

the simple way I use to connect :

    if (mongoose.connection.readyState === 0) {
      try {
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.yellow('connecting...')}`);
        await mongoose.connect(`${mongoUrl}`, { serverSelectionTimeoutMS: 5000 }); // Increased timeout
        if (mongoose.connection.readyState === 1){
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.green('Successfully connected')}`);
      } else {
        console.log(`STATE OF THE DB HEREEEEEEEEEEEE : ${mongoose.connection.readyState}`)
      } 
      } catch (err) {
        console.error(err);
      }
    } else {
      console.log('Already connected to database.');
    }
  }```
@Sigmaxii Sigmaxii added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Feb 27, 2024
@FaizBShah
Copy link
Contributor

Other than the error, what are the logs its printing? Like, is it printing any of the above logs mentioned in the code? Also, is it working by just doing await mongoose.connect()?

@Sigmaxii
Copy link
Author

it does console log that its connected, and it always worked, what else should I do. and this is the only error I am getting

@FaizBShah
Copy link
Contributor

FaizBShah commented Feb 27, 2024

It looks like the issue is with the code where you are using the findOne() option. Can you share that code? If possible, try to share a reproducible code

@Sigmaxii
Copy link
Author

const mongoose = require('mongoose');
const mongoUrl = require('../config.js').mongourl;
const { ChalkAdvanced } = require('chalk-advanced');
const schedule = require('node-schedule');

const dataSchema = new mongoose.Schema({
  key: String,
  value: mongoose.Schema.Types.Mixed
});

const Data = mongoose.model('AutoDelete', dataSchema);

class Database {
  constructor() {
    this.db = mongoose.connection;
    this.db.on('error', console.error.bind(console, 'MongoDB connection error:'));
    this.scheduleCleanup('messagesDeleted', '0 0 * * 0'); // Once a week (Sunday)
    this.scheduleCleanup('messagesDeletedMonth', '0 0 1 * *'); // First day of every month
    this.scheduleCleanup('messagesDeletedYear', '0 0 1 1 *'); // First day of every year
  }
  
  
  async connect() {
    if (mongoose.connection.readyState === 0) {
      try {
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.yellow('connecting...')}`);
        await mongoose.connect(`${mongoUrl}`, { serverSelectionTimeoutMS: 5000 }); // Increased timeout
        if (mongoose.connection.readyState === 1){
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.green('Successfully connected')}`);
      } else {
        console.log(`STATE OF THE DB HEREEEEEEEEEEEE : ${mongoose.connection.readyState}`)
      } 
      } catch (err) {
        console.error(err);
        throw err;
      }
    } else {
      console.log('Already connected to database.');
    }
  }

  async disconnect() {
    if (mongoose.connection.readyState !== 0) {
      try {
        await mongoose.disconnect();
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.red('has been disconnected')}`);
      } catch (err) {
        console.error(err);
      }
    } else {
      console.log('Already disconnected from database.');
    }
  }

  async set(key, value) {
    const existingData = await Data.findOne({ key: key }).exec();
  
    if (existingData) {
      existingData.value = { ...existingData.value, ...value };
      await existingData.save();
    } else {
      const newData = new Data({
        key: key,
        value: value
      });
      await newData.save();
    }
  }

  async autodelete(key, value, forceSetToZero = false) {
    const existingData = await Data.findOne({ key: key }).exec();

    if (existingData) {
      if (forceSetToZero) {
        // If forceSetToZero is true, set the value to zero regardless of the existing value
        existingData.value = 0;
      } else if (typeof existingData.value === 'number') {
        // If the existing value is a number, add the new value to it
        existingData.value += value;
      } else {
        // If the existing value is not a number, set it to the new value
        existingData.value = value;
      }

      await existingData.save();
    } else {
      // If the key doesn't exist, create a new document with the specified value
      const newData = new Data({
        key: key,
        value: value
      });

      await newData.save();
    }
  }


  async get(key) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      return data.value;
    } else {
      return undefined;
    }
  }

  async add(key, value) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      data.value += value;
      await data.save();
    } else {
      const newData = new Data({
        key: key,
        value: value
      });
      await newData.save();
    }
  }

  async delete(key) {
    await Data.findOneAndDelete({ key: key }).exec();
  }

  async push(key, values) {
    const data = await Data.findOne({ key: key }).exec();
  
    if (data) {
      // Ensure that 'value' is an object
      if (typeof data.value === 'object') {
        // Check if the specified properties exist in the object
        for (const property in values) {
          if (values.hasOwnProperty(property)) {
            if (!data.value.hasOwnProperty(property)) {
              // If the property doesn't exist, create it as an array
              data.value[property] = [];
            }
  
            // Use $addToSet to add unique values to the array
            await Data.updateOne({ key: key }, { $addToSet: { [`value.${property}`]: values[property] } });
          }
        }
      } else {
        // If 'value' is not an object, create a new object with the specified values
        const newData = new Data({
          key: key,
          value: values
        });
        await newData.save();
      }
    } else {
      // If the key doesn't exist, create a new document with the specified values
      const newData = new Data({
        key: key,
        value: values
      });
      await newData.save();
    }
  }
  
  
  
  
  
  

  async pull(key, value) {
    await Data.updateOne({ key: key }, { $pull: { value: value } });
  }

 
  async remove(key, values) {
    const data = await Data.findOne({ key: key }).exec();
  
    if (data && typeof data.value === 'object') {
      // Ensure that 'value' is an object
      if (typeof data.value === 'object') {
        // Check if the specified properties exist in the object
        for (const property in values) {
          if (values.hasOwnProperty(property)) {
            // Use $pull to remove the specified values from the array
            await Data.updateOne({ key: key }, { $pull: { [`value.${property}`]: values[property] } });
          }
        }
      }
  
      // Save the updated document
      await data.save();
    }
    // If the key doesn't exist or the property doesn't exist, no action is taken
  }
  
  

  async has(key) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      return true;
    } else {
      return false;
    }
  }

  async hasIn(key, value) {
    const hasIn = await this.get(key);
    if (hasIn && hasIn.includes(value)) {
      return true;
    } else {
      return false;
    }
  }
  

  async scheduleCleanup(collectionName, cronExpression) {
    schedule.scheduleJob(cronExpression, async () => {
      await this.delete(collectionName);
      console.log(`${ChalkAdvanced.blue('Database Cleanup:')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.green(`${collectionName} deleted`)}`);
    });
  }
}

module.exports = Database;

1 similar comment
@Sigmaxii
Copy link
Author

const mongoose = require('mongoose');
const mongoUrl = require('../config.js').mongourl;
const { ChalkAdvanced } = require('chalk-advanced');
const schedule = require('node-schedule');

const dataSchema = new mongoose.Schema({
  key: String,
  value: mongoose.Schema.Types.Mixed
});

const Data = mongoose.model('AutoDelete', dataSchema);

class Database {
  constructor() {
    this.db = mongoose.connection;
    this.db.on('error', console.error.bind(console, 'MongoDB connection error:'));
    this.scheduleCleanup('messagesDeleted', '0 0 * * 0'); // Once a week (Sunday)
    this.scheduleCleanup('messagesDeletedMonth', '0 0 1 * *'); // First day of every month
    this.scheduleCleanup('messagesDeletedYear', '0 0 1 1 *'); // First day of every year
  }
  
  
  async connect() {
    if (mongoose.connection.readyState === 0) {
      try {
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.yellow('connecting...')}`);
        await mongoose.connect(`${mongoUrl}`, { serverSelectionTimeoutMS: 5000 }); // Increased timeout
        if (mongoose.connection.readyState === 1){
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.green('Successfully connected')}`);
      } else {
        console.log(`STATE OF THE DB HEREEEEEEEEEEEE : ${mongoose.connection.readyState}`)
      } 
      } catch (err) {
        console.error(err);
        throw err;
      }
    } else {
      console.log('Already connected to database.');
    }
  }

  async disconnect() {
    if (mongoose.connection.readyState !== 0) {
      try {
        await mongoose.disconnect();
        console.log(`${ChalkAdvanced.blue('Database: ')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.red('has been disconnected')}`);
      } catch (err) {
        console.error(err);
      }
    } else {
      console.log('Already disconnected from database.');
    }
  }

  async set(key, value) {
    const existingData = await Data.findOne({ key: key }).exec();
  
    if (existingData) {
      existingData.value = { ...existingData.value, ...value };
      await existingData.save();
    } else {
      const newData = new Data({
        key: key,
        value: value
      });
      await newData.save();
    }
  }

  async autodelete(key, value, forceSetToZero = false) {
    const existingData = await Data.findOne({ key: key }).exec();

    if (existingData) {
      if (forceSetToZero) {
        // If forceSetToZero is true, set the value to zero regardless of the existing value
        existingData.value = 0;
      } else if (typeof existingData.value === 'number') {
        // If the existing value is a number, add the new value to it
        existingData.value += value;
      } else {
        // If the existing value is not a number, set it to the new value
        existingData.value = value;
      }

      await existingData.save();
    } else {
      // If the key doesn't exist, create a new document with the specified value
      const newData = new Data({
        key: key,
        value: value
      });

      await newData.save();
    }
  }


  async get(key) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      return data.value;
    } else {
      return undefined;
    }
  }

  async add(key, value) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      data.value += value;
      await data.save();
    } else {
      const newData = new Data({
        key: key,
        value: value
      });
      await newData.save();
    }
  }

  async delete(key) {
    await Data.findOneAndDelete({ key: key }).exec();
  }

  async push(key, values) {
    const data = await Data.findOne({ key: key }).exec();
  
    if (data) {
      // Ensure that 'value' is an object
      if (typeof data.value === 'object') {
        // Check if the specified properties exist in the object
        for (const property in values) {
          if (values.hasOwnProperty(property)) {
            if (!data.value.hasOwnProperty(property)) {
              // If the property doesn't exist, create it as an array
              data.value[property] = [];
            }
  
            // Use $addToSet to add unique values to the array
            await Data.updateOne({ key: key }, { $addToSet: { [`value.${property}`]: values[property] } });
          }
        }
      } else {
        // If 'value' is not an object, create a new object with the specified values
        const newData = new Data({
          key: key,
          value: values
        });
        await newData.save();
      }
    } else {
      // If the key doesn't exist, create a new document with the specified values
      const newData = new Data({
        key: key,
        value: values
      });
      await newData.save();
    }
  }
  
  
  
  
  
  

  async pull(key, value) {
    await Data.updateOne({ key: key }, { $pull: { value: value } });
  }

 
  async remove(key, values) {
    const data = await Data.findOne({ key: key }).exec();
  
    if (data && typeof data.value === 'object') {
      // Ensure that 'value' is an object
      if (typeof data.value === 'object') {
        // Check if the specified properties exist in the object
        for (const property in values) {
          if (values.hasOwnProperty(property)) {
            // Use $pull to remove the specified values from the array
            await Data.updateOne({ key: key }, { $pull: { [`value.${property}`]: values[property] } });
          }
        }
      }
  
      // Save the updated document
      await data.save();
    }
    // If the key doesn't exist or the property doesn't exist, no action is taken
  }
  
  

  async has(key) {
    const data = await Data.findOne({ key: key }).exec();
    if (data) {
      return true;
    } else {
      return false;
    }
  }

  async hasIn(key, value) {
    const hasIn = await this.get(key);
    if (hasIn && hasIn.includes(value)) {
      return true;
    } else {
      return false;
    }
  }
  

  async scheduleCleanup(collectionName, cronExpression) {
    schedule.scheduleJob(cronExpression, async () => {
      await this.delete(collectionName);
      console.log(`${ChalkAdvanced.blue('Database Cleanup:')} ${ChalkAdvanced.gray('>')} ${ChalkAdvanced.green(`${collectionName} deleted`)}`);
    });
  }
}

module.exports = Database;

@Sigmaxii
Copy link
Author

That is it , and I use the connect here :

const { ShardingManager } = require('discord.js');
const fs = require('fs');
const config = require('./src/config.js');
const { ChalkAdvanced } = require('chalk-advanced');
const manager = new ShardingManager('./main.js', { token: config.token, respawn: true });
const on = require("./website/server.js");
const { Auto } = require('./src/structures/bot.js');
const abot = new Auto();
const Database = require("./src/utils/Database.js");
const db = new Database();
db.connect();
on(abot);
//...

@donoftime2018
Copy link

Why not use then() or await/async for where the mongoose connection statement?

@Sigmaxii
Copy link
Author

Other than the error, what are the logs its printing? Like, is it printing any of the above logs mentioned in the code? Also, is it working by just doing await mongoose.connect()?

the console.logs are printing that its connecting succesfully, and then the error pops, after a few seconds

@ghost-dev-gr
Copy link

Does the connection appear on the dashboard of mongoose??

@Sigmaxii
Copy link
Author

Does the connection appear on the dashboard of mongoose??

yes !? (if u mean the metrics on mongodb cloud)

@Sigmaxii
Copy link
Author

After some testing, indeed findOne gets timed out. but I dont know why tho. findOne is still a function, and I assume that the code is correctly written (which obviously is not since I have this bugs)

@FaizBShah
Copy link
Contributor

@Sigmaxii Maybe this stack overflow answer might help you - https://stackoverflow.com/questions/65408618/mongooseerror-operation-users-findone-buffering-timed-out-after-10000ms.

Also, before that, can you allow access from everywhere (0.0.0.0) instead of just your IP address to check whether its not an issue due to your IP or VPS

@donoftime2018
Copy link

Are you connecting using 127.0.0.1 or localhost?

@vkarpov15
Copy link
Collaborator

Is there any indication that your code called disconnect()?

Also, can you please paste the output of npm list mongoose?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Feb 28, 2024
@Sigmaxii
Copy link
Author

Sigmaxii commented Mar 1, 2024

Is there any indication that your code called disconnect()?

Also, can you please paste the output of npm list mongoose?

Currently no, disconnect is not called anywhere, and as mentioned in the description, mongoose version is 8.2

@Sigmaxii
Copy link
Author

Sigmaxii commented Mar 2, 2024

@Sigmaxii Maybe this stack overflow answer might help you - https://stackoverflow.com/questions/65408618/mongooseerror-operation-users-findone-buffering-timed-out-after-10000ms.

Also, before that, can you allow access from everywhere (0.0.0.0) instead of just your IP address to check whether its not an issue due to your IP or VPS

  • About the stack overflow:
  • This is not helping since its all about mongoose 5.x
  • About the IP access:
  • Yes I do allow access from everywhere thats not the issue

@vkarpov15
Copy link
Collaborator

"mentioned in the description, mongoose version is 8.2" <-- I understand, I'm asking for the output of npm list mongoose to rule out multiple versions of Mongoose.

Also, are you connecting to localhost or a remote server?

Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Mar 20, 2024
Copy link

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity Stale
Projects
None yet
Development

No branches or pull requests

5 participants