Reputation: 541
I'm trying to remove a nested object, but for some reason I'm doing something wrong as it seems that I cant access the nested object. I can delete objects that aren't nested.
Here is my code
Json:
{
"_id": ObjectId("55904d2da35bf71c06184f60"),
"title": "h1",
"url": "h1",
"menuIndex": NumberInt(0),
"date": ISODate("2015-06-28T21:12:38.256Z"),
"contents": [
{
"order": NumberInt(0),
"data": "something",
"_id": ObjectId("55904d38a35bf71c06184f62")
},
{
"order": NumberInt(1),
"data": "not something",
"_id": ObjectId("55904d49a35bf71c06184f63")
}
],
"__v": NumberInt(0)
}
Api:
router.post('/content/delete/:id/:contentId', sessionCheck, function(req,res) {
var id = req.body._id;
var contentId = req.body._id;
Page.update({
_id: id
}, {
$pull: {'contents': {
_id: contentId
}}},
function(err){
if(err)
return res.send(err);
}
);
});
Factory:
.factory('pagesFactory', ['$http',
function($http) {
return {
deleteContent: function(id,contentId) {
return $http.post('/api/content/delete/'+ id + '/' + contentId);
}
};
}
])
Controller:
$scope.deleteContent = function(id,contentId) {
pagesFactory.deleteContent(id,contentId);
};
HTML:
<div ng-repeat="data in pageContent.contents track by data.order">
<button class="btn btn-success" ng-click="deleteContent(pageContent._id,data._id)">Delete</button>
<div ng-bind-html="trustAsHtml((data.data))">
</div>
</div>
Upvotes: 0
Views: 685
Reputation: 17498
Both id
and contentId
are sent as url params not POST body. So the correct code is:
var id = req.params.id; // not req.body._id
var contentId = req.params.contentId; // not req.body._id
Page.update({
_id: id
}, {
$pull: {'contents': {
_id: contentId
}}},
function(err){
if(err)
return res.send(err);
}
);
Upvotes: 2