Kevin Kevin - 3 months ago 9
Node.js Question

How to delete objects present in array in angular.js

I want to delete a product present in wishlist array. The index zero item is deleted successfully but index one is not getting deleted.

Image of Result

When I try to delete index zero item I am getting wishlist Id, product Id, and index of that product but for the second one, I am not getting a wishlist Id.

wishlist.html



<md-card class="md-whiteframe-3dp" ng-repeat="product in vm.rslt1" flex="40" style="max-width: 50%;">
<div layout="row" layout-align="end center">
<md-button ng-click="deletePrdct(vm.rslt1[$index]._id, product.Product_Id, $index)" aria-label="Delete" class="md-icon-button md-mini">
<md-icon md-svg-icon="/assets/icons/fonts/delete.svg" aria-label="Delete"></md-icon>
<md-tooltip md-direction="bottom">Remove from Wishlist</md-tooltip>
</md-button>
</div>
<img ng-src="{{ product.Image }}" class="md-card-image" alt="Image here" style="margin-left:80px">

<div style="margin-left:15px">
<h2>&#8377;{{ product.Price }}</h2>
</div>

<div style="margin-left:15px">
<h2>{{product.Product_name | characters:30}}</h2>
</div>

<md-card-content>
<p align="justify" style="text-indent: 30px">
{{ product.Description | characters:150}}
</p>
</md-card-content>
</md-card>


wishlistController.js



vm.showItems = function(id){
vm.wishListId = id;
$http({
url: 'http://192.168.2.8:7200/api/getItemOfWishList',
method: 'POST',
data: {wID: vm.wishListId}
}).success(function(res) {
vm.rslt = res.result;
//console.log(vm.rslt);
vm.rslt1 = res.result1;
console.log(vm.rslt1);
vm.count = vm.rslt.length;
if(vm.count == 0){
vm.showMessage = true;
} else {
vm.rslt=res.result;
vm.showMessage = false;
}
}, function(error) {
console.log(error);
alert('here');
});
}


$scope.deletePrdct = function(wId, pId, rindex){
//$scope.vm.rslt.splice(rindex, 1);

console.log(wId);
console.log(pId);
console.log(rindex);

var ifYesPressed = $window.confirm("Are you sure you want to delete?");
if(ifYesPressed === true){
$http({
url: 'http://192.168.2.8:7200/api/wishlistProductDelete',
method: 'POST',
data:{wishlistId: wId, productId: pId, userId:vm.uid},
}).success(function(res) {
console.log('deleted');
//refresh();
}, function(error) {
console.log(error);
alert('here');
});

var index = $scope.vm.rslt.indexOf(pId);
$scope.vm.rslt.splice(rindex, 1);
}
}


api.js



router.post('/getItemOfWishList', function(req,res){
var wId = ObjectId(req.body.wID);
//console.log(wId);
var findwishlists = function(db, callback) {
var cursor =db.collection('wishlists').find({_id:wId}).toArray(function(err, docs){
//console.log(docs);
if(err){
callback(new Error("Some problem"));
}else{
callback(null,docs);
}
});
};

MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
findwishlists(db, function(err,docs) {
db.close();
for(var key in docs){
var products = docs[0].productsInWishlists;
return res.json({result: products, result1: docs});
}
});
});
});

Answer

I think your problem may be here:

for(var key in docs){
            var products = docs[0].productsInWishlists;     
            return res.json({result: products, result1: docs});
            }

result1 contains the whole docs object.

result contains the productsInWishlists item of the first object of your docs array

Double check if this is what you want to accomplish.

Comments