Currently, I am using Express along with Mongoose for my project. I have encountered an issue with filtering an array obtained from a database. The objects retrieved from the database are structured differently compared to the object format in my request. This discrepancy seems to be causing issues with the filtering process. How can this be resolved?
router.post("/deleteTask", auth, async (req, res) => {
try {
const user = await User.findById(req.user);
console.log(req.body.task);
console.log(user.tasks.filter(task => task !== req.body.task));
await User.updateOne(
{ _id: req.user },
{ tasks: user.tasks.filter(task => task !== req.body.task) },
{ upsert: true }
);
res.json(true);
} catch (error) {
res.json({
message: error.message
});
}
});
The first console.log displays
{ title: 'test888', description: '' }
while the second console.log shows
... {"title":"test888","description":""} ...