Search code examples
javascriptmongodbdiscord.js

Why document is not deleting in the mongodb?


The problem that occurs is the user is not getting removed from the document in the database. The user is named mention.

This is the ingroups schema that im using

const mongoose = require("mongoose")

const groupSchema = mongoose.Schema({
    User:String,
    GroupName:String,
    TotalAmountDonated:Number,
})

module.exports = mongoose.model("InGroup", groupSchema, 'InGroups')

This is where the mention variable comes from

 .addSubcommand(subcommand =>
             subcommand
            .setName('kick')
            .setDescription('Kick an unfortunate group member')
            .addUserOption(option =>
              option
            .setName('groupmember')
            .setDescription('Group member you would like to kick')
            .setRequired(true))),

These are the if statements and for some reason the findOneAndDelete line is not working

const mention = interaction.options.getUser('groupmember');
const data1 = await ingroups.findOne({ User: mention.id });
if (data1 !== null) {
  if (data1.GroupName !== user.GroupName) {
    return await interaction.reply(
      'The person you are attempting to kick is not in your group.'
    );
  }
  await ingroups.findOneAndDelete({ User: mention.id });
  return await interaction.reply(
    `<@${mention.id}> has been successfully kicked from the group`
  );
} else {
  return await interaction.reply(
    'The user you are trying to kick is not in a group!'
  );
}

Solution

  • You already defined the group you want to delete.

    Try: await data1.delete();