'Mongoose.js not removing object from array

I'm using Mongoose with my Discord bot and I'm making the feature where the infractions can be removed from any mentioned user(s).

The infractions are saved in an Array

However, when trying to remove the object from the array, nothing happens, no error.

Here is my code

What foundCase is defined as (returns this object)

{
  type: 'warn',
  caseNum: 6,
  userID: '300816697282002946',
  responsibleModerator: '300816697282002946',
  reason: 'playing minecraft',
  date: 1592678689923
}

My code

let foundCase = message.guild.data.infractions.find(c => {
                        return c.userID === calledMember.user.id && c.caseNum === Number(caseNum);
                    })

                    if (!foundCase)
                        return message.channel.send(`There were no cases found with that ID to remove from this user. Please ensure the case number is correct, and the user you are mentioning is the right user.`)



                    console.log(foundCase)
                    await client.guildData.updateOne( { guildData: message.guild.id }, { $pull: { infractions: foundCase }})
                    message.channel.send(`I have successfully deleted case number \`${caseNum}\`, and removed the infraction from the user. The change will reflect in any reports within 5 mins.`)
                

However, nothing happens. The infraction is not removed at all and is still in the array.

Does anybody have any suggestions?



Solution 1:[1]

it maybe the case that your foundCase returns and empty object if no Id matches so to be safe change this line if (!foundCase) to this if(foundCase.userId=="undefined")

also you can try this

client.guildData.updateOne( { guildData: message.guild.id }, { $pull: { infractions: "foundCase"}})

or this

client.guildData.updateOne( { guildData: message.guild.id }, { $pull:{infractions: { $in: [ "foundCase"] }}})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven.hig