MongooseError:Model.findOne() 不再接受 Function 的回调

aki*_*kim 10 javascript mongoose mongodb node.js discord.js

我在设置 mongoose 时遇到了问题。

这是我的代码:

const { SlashCommandBuilder } = require('@discordjs/builders');
const testSchema = require(`../../Schemas.js/test`);

module.exports = {
    data: new SlashCommandBuilder()
    .setName('dbtest')
    .setDescription('db test'),
    async execute(interaction) {

        testSchema.findOne({ GuildID: interaction.guild.id, UserID: interaction.user.id}, async(err, data) => {
            if (err) throw err;

            if (!data) {
                testSchema.create({
                    GuildID: interaction.guild.id,
                    UserID: interaction.user.id
                })
            }

            if (data) {
                console.log(data)
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

我的错误:

/Users/akimfly/akim-slash-bot/node_modules/mongoose/lib/model.js:2131
    throw new MongooseError('Model.findOne() no longer accepts a callback');
          ^

MongooseError: Model.findOne() no longer accepts a callback
    at Function.findOne (/Users/akimfly/akim-slash-bot/node_modules/mongoose/lib/model.js:2131:11)
    at Object.execute (/Users/akimfly/akim-slash-bot/src/commands/Community/databasetest.js:10:20)
    at Object.execute (/Users/akimfly/akim-slash-bot/src/events/interactionCreate.js:12:21)
    at Client.<anonymous> (/Users/akimfly/akim-slash-bot/src/functions/handleEvents.js:8:58)
    at Client.emit (node:events:513:28)
    at InteractionCreateAction.handle (/Users/akimfly/akim-slash-bot/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (/Users/akimfly/akim-slash-bot/node_modules/discord.js/src/client/websocket/handlers/INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (/Users/akimfly/akim-slash-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (/Users/akimfly/akim-slash-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:489:22)
    at WebSocketShard.onMessage (/Users/akimfly/akim-slash-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:328:10)
Run Code Online (Sandbox Code Playgroud)

Zso*_*ros 13

从 5.0 版本开始,Mongo 放弃了对其 Node.js 驱动程序回调的支持,转而采用仅 Promise 的公共 API。Mongoose 还在 v7 中放弃了回调支持,因此findOne()其他方法现在总是返回一个承诺。

可以在此处找到受影响方法的完整列表。

您可以使用 async/await 代替:

module.exports = {
  data: new SlashCommandBuilder().setName('dbtest').setDescription('db test'),
  async execute(interaction) {
    try {
      const data = await testSchema.findOne({
        GuildID: interaction.guild.id,
        UserID: interaction.user.id,
      });

      if (!data) {
        testSchema.create({
          GuildID: interaction.guild.id,
          UserID: interaction.user.id,
        });
      }

      if (data) {
        console.log(data);
      }
    } catch (error) {
      console.log(error);
    }
  },
};
Run Code Online (Sandbox Code Playgroud)

或者只是好旧then的:

module.exports = {
  data: new SlashCommandBuilder().setName('dbtest').setDescription('db test'),
  execute(interaction) {
    testSchema
      .findOne({
        GuildID: interaction.guild.id,
        UserID: interaction.user.id,
      })
      .then((data) => {
        if (!data) {
          testSchema.create({
            GuildID: interaction.guild.id,
            UserID: interaction.user.id,
          });
        }

        if (data) {
          console.log(data);
        }
      })
      .catch((err) => console.log(err));
  },
};
Run Code Online (Sandbox Code Playgroud)


小智 7

MongooseError:Model.find() 不再接受回调

*你可以使用async/await或promise(then)

Model.find().then((data) => {
 console.log(data);
....
})
Run Code Online (Sandbox Code Playgroud)