NodeJS MongooseDB错误:操作级别.findOne()缓冲在10000ms后超时

3htmauhk  于 2023-01-01  发布在  Node.js
关注(0)|答案(1)|浏览(299)

我正在尝试为我的机器人(discord.js)做一个水平系统,我正在使用mongoose作为数据库。每当我运行命令时,机器人崩溃,这是我得到的错误:

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)

下面是我的代码:

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] });
    }
}

机器人将崩溃并发送消息"应用程序没有响应"。
有人知道如何解决这个问题吗?我已经在网上看过了,没有显示这个错误。

8dtrkrch

8dtrkrch1#

在建立连接之前调用模型。
根据文件
Mongoose允许您立即开始使用模型,而无需等待Mongoose建立到MongoDB的连接。
使用带async wait的mongoose.connect(uri)与数据库建立连接,然后再次运行该命令。

相关问题