Home > database >  MongooseError: Query was already executed: Error: Discord.js
MongooseError: Query was already executed: Error: Discord.js

Time:05-24

Code

const fetch = require("node-fetch");


client.on('messageCreate', async (message) => {
    const chatbots = require("./All-Commands-Schemas/ChatbotSchema")
    await chatbots.findOne({ guild: message.guild.id }, async (err, data) => {
        if (!data) return;
        if (err) throw err
        const channell = data.channel
        if (message.author.bot || message.channel.type === 'dm') return;

        if (message.channel.id === channell) {
            message.channel.sendTyping();
            await fetch(`http://api.brainshop.ai/get?bid=163&key=rztKzYO&uid=${message.author.id}&msg=${message.content}`)
                .then(cnt => cnt.json())
                .then(data => {
                    message.channel.send(data.cnt);
                })
                .catch(() => {
                    message.channel.send("Couldn't fetch response!");
                })
        };
    });
});

Error

Reason: MongooseError: Query was already executed: chatbots.findOne(

Hi I need help with the following error and code which is provided above. Below are the versions of my code.

"mongoose": "^6.1.6", "discord.js": "^13.3.1", "node": "16.13.2", "npm": "8.1.2"

CodePudding user response:

You cannot use await and callback in same query, you should remove await

chatbots.findOne({ guild: message.guild.id }, async (err, data) => {
//do some thing
}

or callback

const data = awaitchatbots.findOne({ guild: message.guild.id })
//do something
  • Related