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

Mongoose dropped support for callbacks from its node.js driver as of version 5.0. You can use async/await instead:

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);
    }
  },
};

Leave a Comment