trying to remove an object from array after using aggregate(). but im getting this error.
‘subCategory.remove is not a function’
const deleteSubCategory = asyncHandler(async (req, res) => {
const subCategory = await Category.aggregate([{$unwind: "$SubCats"}, { $replaceRoot: {newRoot: '$SubCats'}}, {$match: {_id: ObjectId(req.params.id)}}])
if (subCategory) {
await subCategory.remove()
res.json({ message: 'sub-category removed' })
} else {
res.status(404)
throw new Error('sub-Category not found')
}
})
results after aggregation:
[{
_id: '61cae5daf5bfbebd7cf748ef'
name: 'subcat 1',
image: '/assets/images/vr-box-6203301_1920.jpg',
}]
array before aggregation:
[
{
_id: '61cae5daf5bfbebd7cf748ee'
title: 'category 1',
SubCats: [
{
_id: '61cae5daf5bfbebd7cf748ef'
name: 'subcat 1',
image: '/assets/images/vr-box-6203301_1920.jpg',
},
{
_id: '61cae5daf5bfbebd7cf748fb'
name: 'subcat 2',
image: '/assets/images/galaxy-s20_highlights_kv_00.jpg',
},
]
},
]
is it saying ‘subCategory.remove is not a function because of the ‘[]’ in the results after aggregation. becasue i find that remove()
works fine if the reulsts start with {}