Search code examples
mongodbdiscord.jstopology

MongoError: Topology is closed, please connect when making a discord.jsv12 mute command


I've seen many people get the Topology is closed error, but I've never seen a straight answer that doesn't only answer the question for the exact code snippet given, but generally gives an overview of what the topology is and how it works.

I'm following the WOK DiscordJSv12 tutorials on YT, and have the mute command cloned from the repo and added some tweaks to include embeds. But as soon as I ran the code i got the topology error.

Here is the file where I get the error:

const muteSchema = require('@schemas/mute-schema')

module.exports = (client) => {
    const checkMutes = async () => {
        console.log('CHECKING MUTE DATA')

        const now = new Date()

        const conditional = {
            expires: {
                $lt: now
            },
            current: true,
        }
//The error is on this next line, it doesn't like that 'await'
        const results = await muteSchema.find(conditional)

        if (results && results.length) {
            for (const result of results) {
                const { guildId, userId } = result

                const guild = client.guilds.cache.get(guildId)
                const member = (await guild.members.fetch()).get(userId)

                const mutedRole = guild.roles.cache.find((role) => {
                    return role.name === 'Muted'
                })

                member.roles.remove(mutedRole)
            }

            await muteSchema.updateMany(conditional, {
                current: false,
            })
        }

        setTimeout(checkMutes, 1000 * 60 * 10 )
    }
    checkMutes()

    client.on('guildMemberAdd', async (member) => {
        const { guild, id } = member

        const currentMute = await muteSchema.findOne({
            userId: id,
            guildId: guild.id,
            current: true,
        })

        if (currentMute) {
            const role = guild.roles.cache.find((role) => {
                return role.name === 'Muted'
            })

            if (role) {
                member.roles.add(role)
            }
        }
    })
}

Here's the other file that completes the logic of this file: link

Also here is the schema: link

And my mongo file that connects to mongo: link

So I'd like to know how to fix my issue, and also for future errors how I can solve it myself and what the error basically means, since there is no exact line of my own code specified in the error.


Solution

  • Whenever you encounter these types of errors as mentioned: MongoErrror: Topology is closed, they refer to an unestablished connection to your Mongo Database, as directed by your comment I could figure out that you never called your connection function in @util/mongo.js file that you were using to connect to MongoDB. To get over this, you may call the function before finding the results and further close it ( good practice ) here's how:

    
        const mongo = require("@util/mongo.js"); // reference from WOK source code ( he hasn't added this but filepath would be somewhat this, you can add the filepath to your connection function.
        await mongo().then(async (mongoose) => {
              try {
        const results = await muteSchema.find(conditional)
        ... // Your code to utilise this result 
        } finally {
        mongoose.connection.close()
        }
    
    

    Work Off Keys may have forgotten to mention this as a common error in many of their tutorials therefore they have an unlisted video for it here. You may be able to fix your this error yourselves most of the time it occurs now 😄.