I have the data like this
{
"_id": "61794cfbbe84b5f1f8ae4e16",
"image": "www.imagetest.com",
"title": "test pembuatan artikel",
"category": [
"informasi",
"budidaya"
],
"description": "tes artikel",
"createdAt": "2021-10-27T12:58:35.969Z",
"updatedAt": "2021-11-06T01:49:06.012Z",
"__v": 0
}
i want to remove some category from category arrays by selected _id. I have tried $pull but it does not change in my database. I've tried with $pull update method like this:
deleteCategory: async (req, res, next) => {
const { articleId } = req.params
const { category } = req.body.category
try {
await Article.updateOne(
{_id: articleId},
{ $pull: {category: category}}
)
res.json({
message: 'Article deleted successfully!',
})
} catch (error) {
res.status(500).json({
message: 'An error Occured!',
})
}
}
with this route:
router.delete('/category/delete/:articleId', ArticleController.deleteCategory)
Anyone knows what i'm missing?
i dont know what just happen, but i tried this and it works. I'm pretty sure i've tried this before and it's not working, but i tried this again and it works
const { articleId } = req.params
try {
await Article.updateOne(
{_id: articleId},
{ $pull: {category: req.body.category}}
)
res.json({
message: 'Article deleted successfully!',
})
} catch (error) {
res.status(500).json({
message: 'An error Occured!',
})
}