I have a scenario in my task management application where I want to remove completed tasks from the MongoDB database when a logged-in user marks them as done. Below is the snippet of code for my Schema.
const user = new mongoose.Schema({
username : String,
password : String,
task : [{
text : String,
day : String,
reminder : Boolean,
]}
})
As an example, let's say Daryl completes the task with text : "Gym" & day : "Feb 4th 5.30pm". In such cases, I only want to delete the specific task entry from Daryl's task array.
Below is my attempt to achieve this using Mongoose,
app.delete("/tasks", (req,res) => {
User.findOne( {_id : req.user.id}).then((target) => {
target.task.remove({text : req.body.text, day : req.body.day})
})
})
- Use User.findOne({_id : req.user.id}) to target the logged-in user
- Access the task array by using .task once targeted
- Utilize .remove along with filters to eliminate the specific task from the array
Even after logging all the variables, which match with the data fields, the task entry is not being removed. Can anyone help me identify what mistake I may be making here?