Search code examples
mongodbmongoosemongoose-schema

MongooseError: Model.findOne() no longer accepts a callback


Error:

    throw new MongooseError('Model.findOne() no longer accepts a callback');
          ^

MongooseError: Model.findOne() no longer accepts a callback
    at Function.findOne (/Users/roopa/Desktop/projects/LLbot/node_modules/mongoose/lib/model.js:2150:11)
    at Object.execute (/Users/roopa/Desktop/projects/LLbot/src/Commands/moderation/setup-logs.js:16:19)
    at Object.execute (/Users/roopa/Desktop/projects/LLbot/src/events/interactions/interactionCreate.js:70:13)
    at Client.<anonymous> (/Users/roopa/Desktop/projects/LLbot/src/handlers/eventHandler.js:50:63)
    at Client.emit (node:events:523:35)
    at InteractionCreateAction.handle (/Users/roopa/Desktop/projects/LLbot/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (/Users/roopa/Desktop/projects/LLbot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (/Users/roopa/Desktop/projects/LLbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:354:31)
    at WebSocketManager.<anonymous> (/Users/roopa/Desktop/projects/LLbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:238:12)
    at WebSocketManager.emit (/Users/roopa/Desktop/projects/LLbot/node_modules/@vladfrangu/async_event_emitter/dist/index.js:282:31)

Node.js v20.2.0

I cannot send the whole code but i'm pretty sure this part is giving error:

function send_log(guildId, embed) {
        logSchema.findOne({ Guild: guildId }, async (err, data) => {
            if (!data || !data.Channel) return;
            const LogChannel = client.channels.cache.get(data.Channel);
            
            if(LogChannel) return;
            embed.setTimestamp();
            
            try {
            LogChannel.send({ embeds: [embed] });
            } catch(err) {
                console.log(err);
            }
        });
    }

i was having trouble with _patch() function so i just commented that part out and now it works but idk how to fix this one. I know that Mongoose just dropped support for callbacks from its node.js driver as of version 5.0. but idk how to


Solution

  • As stated by the error, the findOne method no longer accepts a callback parameter. You can just use async await:

    async function send_log(guildId, embed) {
        try {
            const data = await logSchema.findOne({ Guild: guildId });
            if (!data || !data.Channel) return;
            const LogChannel = client.channels.cache.get(data.Channel);
    
            if (LogChannel) return;
            embed.setTimestamp();
    
            LogChannel.send({ embeds: [embed] });
        } catch (err) {
            console.log(err);
        }
    }