两次运行命令时出现 Discord.js Mongo 错误

时间:2021-02-04 08:50:36

标签: mongodb mongoose discord

await mongoose.connect(mongoPath, {useNewUrlParser: true, useUnifiedTopology: true}).then( async (mongoose) => {
    try {
       let guildObj = await myschema.findById({_id: guild.id})
       // I Check if Channel can be used
       if (guildObj.channel.beingUsed == true) return message.channel.send(`Someone else if already using this on this channel`)
       // Here I Then Run All My Code  (and I update the schema too)
       commandStarted = true // etc...
    
    }finally{
       if (commandStarted == true) {
           mySchema.findByIdAndUpdate() // I update the beingUsed property back to false if command started
       }
       mongoose.connection.close()
    }
})

嗨,这是我的代码的一个非常简化的版本。我基本上让用户运行这个命令:当他们这样做时,机器人连接到数据库,然后我运行我的所有代码。全部完成后,它运行 finally 块关闭连接,并且它可以工作。因为我想让机器人不能被同一频道中的多个用户使用,所以我只在命令运行时更新我的​​架构(命令平均需要 1 分钟才能完成)并在它完成后再次更新它。

当我在命令已经运行的情况下运行命令时,问题就出现了。例如:

我运行了该命令,它运行得非常好,但是当我再次运行该命令时(在第一个命令完成之前),就像它应该工作一样,它向我返回“其他人已经在玩”消息,但随后它有点杀死第一个连接,第一个命令停止工作,给出“缓冲在 10000 毫秒后超时”错误(据我所知,这意味着你想对数据库做一些事情,但你没有连接到它)

我真的不知道为什么会这样,感觉 mongoose.connection.close() 关闭了两个连接,我不明白为什么,因为我认为它只应该关闭一。 所以这可能不是这种情况,我真的不明白我错过了什么。感谢您的帮助 :D

0 个答案:

没有答案