简体   繁体   中英

MongooseDB Error: Operation `levels.findOne()` buffering timed out after 10000ms

I am trying to make a leveling system for my bot (discord.js) and I am using mongoose for the database. Whenever I run the command the bot crashes and this is the error I get:

C:\Users\Liam\Desktop\slashcmds\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:158
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `levels.findOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\Liam\Desktop\slashcmds\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:158:23)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

Here is my code:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilded, AttachmentBuilder, Embed, EmbedBuilder } = require(`discord.js`);
const { Canvacord } = require('canvacord');
const levelSchema = require(`../../Schemas.js/level`);

module.exports = {
    data: new SlashCommandBuilder()
    .setName('rank')
    .setDescription("Get a server member's XP rank")
    .addUserOption(option => option.setName('target').setDescription("This is the user you want to check the XP rank of.").setRequired(false)),
    async execute(interaction) {
        const { options, user, guild } = interaction;
        const Member = options.getUser('target');
        const member = guild.members.cache.get(Member.id);
        const Data = await levelSchema.findOne({ Guild: guild.id, User: member.id });

        const embed = new EmbedBuilder()
        .setColor('Blue')
        .setTitle(`${member.user.username}'s XP Rank`)
        .setDescription(`${member.user.username} has not gained any XP yet.`)

        if(!Data) return await interaction.reply({ embeds: [embed] });
        await interaction.deferRepy();

        const Required = Data.Level * Data.Level * 20 + 20;
        const rank = new Canvacord.rank()
        .setAvatar(member.displayAvatarURL({ forseStatic: true}))
        .setBackground("IMAGE", `https://cdn.discordapp.com/attachments/1055548593186025513/1057353099720798308/OIP.jpg`)
        .setCurrentXP(Data.XP)
        .setRequiredXP(Required)
        .setRank(1, "Rank", false)
        .setLevel(Data.Level, "Level")
        .setUsername(member.user.username)
        .setDiscriminator(member.user.discriminator)

        const Card = await rank.build();
        const attachment = new AttachmentBuilder(Card, { name: "rank.png" });

        const embed2 = new EmbedBuilder()
        .setColor("Blue")
        .setTitle(`${member.user.username}'s XP Rank`)
        .setImage("attachment://rank.png")

        await interaction.editReply({ embeds: [embed2], files: [attachment] });
    }
}

The bot would crash and send the message "Application did not respond".

Anyone knows how to fix this? I've looked online and nothing shows up for this error.

You're calling the model before the connection is established.

According to documentation

Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.

Use mongoose.connect(uri) with async await to establish connection with database and run the command again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM