[英]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.我正在尝试为我的机器人 (discord.js) 制作一个调平系统,我正在为数据库使用 mongoose。 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.在建立连接之前,您正在呼叫 model。
According to documentation根据文档
Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB.
Mongoose 让您可以立即开始使用您的模型,而无需等待 mongoose 与 MongoDB 建立连接。
Use mongoose.connect(uri)
with async await to establish connection with database and run the command again.使用
mongoose.connect(uri)
和 async await 建立与数据库的连接并再次运行命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.