HOW TO FIX Mongoose 5.11.8 model.find() ERROR Operation `thanks-leaderboards.find()` buffering timed out after 10000ms

Viewed 265

How to solve model.find() function produces "buffering timed out after ... ms"? I'm using mongoose v 5.11.8, npm v6.14.8 and mongodb v3.6.3

Here's the code.

thanks-leaderboard.js

const thanksLeaderboardSchema = require('../../schemas/thanks-leaderboard-schema.js')
const thanksSchema = require('../../schemas/thanks-schema.js')

const fetchTopMembers = async (guildId) => {
  let text = ''

  const results = await thanksSchema
    .find({
      guildId,
    })
    .sort({
      received: -1,
    })
    .limit(10)

  for (let counter = 0; counter < results.length; ++counter) {
    const { userId, received = 0 } = results[counter]

    text += `#${counter + 1} <@${userId}> with ${received} thanks\n`
  }

  text += '\nThis is updated every minute'

  return text
}

const updateLeaderboard = async (client) => {

  const results = await thanksLeaderboardSchema.find({}).where('channelId')
  
  for (const result of results) {
    const { channelId, _id: guildId } = result

    const guild = client.guilds.cache.get(guildId)
    if (guild) {
      const channel = guild.channels.cache.get(channelId)
      if (channel) {
        const messages = await channel.messages.fetch()
        const firstMessage = messages.first()

        const topMembers = await fetchTopMembers(guildId)

        if (firstMessage) {
          firstMessage.edit(topMembers)
        } else {
          channel.send(topMembers)
        }
      }
    }
  }

  setTimeout(() => {
    updateLeaderboard(client)
  }, 1000 * 60)
}

module.exports = async (client) => {
  updateLeaderboard(client)
}

set-leaderboard.js

const leaderBoardSchema = require('../../schemas/thanks-leaderboard-schema.js')


module.exports = {

    commands: 'setleaderboard',
    requiredRoles: ['Administrator'],
    description: 'Erstelle ein Leaderboard für die vergebenen Danksagungen',
    callback: async (message) => {

        const { guild, channel} = message
        const guildId = guild.id
        const channelId = channel.id

        await leaderBoardSchema.findOneAndUpdate({
            
            _id: guildId,
            channelId,
          },

          {
            _id: guildId,
            channelId,
          },

          {
            upsert: true,
          })

          message.reply('Leaderboard wurde erstellt!').then((message) => {
            message.delete({
              timeout: 1000 * 5,
            })
          })
          message.delete()
    }
}

thanks-leaderboard-schema.js

const mongoose = require('mongoose')

const reqString = {
  type: String,
  required: true,
}

const thanksLeaderboardSchema = mongoose.Schema({
  // Guild ID
  _id: reqString,
  channelId: reqString,
})

module.exports = mongoose.model('thanks-leaderboards', thanksLeaderboardSchema, 'thanks-leaderboards')

thanks-schema.js

const mongoose = require('mongoose')

const reqString = {
  type: String,
  required: true,
}

const thanksSchema = mongoose.Schema({
  userId: reqString,
  guildId: reqString,
  received: {
    type: Number,
    default: 0,
  },
  lastGave: Date,
})

module.exports = mongoose.model(
  'thanks',
  thanksSchema,
  'thanks'
)

I tried increasing the bufferTimeoutMS or disabling the bufferCommands but still it won't work. This Code is supposed to readout the database every 60 seconds but the timeout happens after 10 seconds... can't figure out how to fix this error. I've already tried to delete the node_modules folders for mongoose and mongodb and reinstall them via npm but that didn't work as I get this error again.

1 Answers

use following code for connect to mongodb

const mongoose = require('mongoose')
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })

after that you should removing the node_module folder and all .json files and reinstalling the mongoose and use npm update

Related