MongooseError: Model.findOne()不再接受回调函数

huangapple go评论69阅读模式
英文:

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

问题

错误:

    throw new MongooseError('Model.findOne()不再接受回调函数');
          ^

MongooseError: Model.findOne()不再接受回调函数
    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

我不能发送整段代码,但我相当确定这部分会导致错误:

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:  });
            } catch(err) {
                console.log(err);
            }
        });
    }

我遇到了_patch()函数的问题,所以我将其注释掉,现在可以运行,但我不知道如何修复这个。我知道Mongoose自5.0版本起不再支持回调函数。

英文:

Error:

    throw new MongooseError(&#39;Model.findOne() no longer accepts a callback&#39;);
          ^

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.&lt;anonymous&gt; (/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.&lt;anonymous&gt; (/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) =&gt; {
            if (!data || !data.Channel) return;
            const LogChannel = client.channels.cache.get(data.Channel);
            
            if(LogChannel) return;
            embed.setTimestamp();
            
            try {
            LogChannel.send({ embeds:  });
            } 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

答案1

得分: 0

根据错误信息,findOne 方法不再接受回调参数。
你可以使用 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);
    }
}

<details>
<summary>英文:</summary>

As stated by the error, the [`findOne`][1] 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:  });
} catch (err) {
    console.log(err);
}

}



  [1]: https://mongoosejs.com/docs/api/model.html#Model.findOne()

</details>



huangapple
  • 本文由 发表于 2023年6月16日 11:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定