0

I need help with removing this item from nested array. I tried used $http.delete but this method deleted whole ObjectID from database, and second problem is that I can't connect the click on the "removing" button with backend code.

My code:

var product = new Schema({
  title: String,
  price: String,
  description: [ObjectID]
});

Index.html

<form name="editProduct.descriptionForm" ng-submit="editProduct.updateDescription(newDescription, editProduct.descriptionForm.description.$valid)" novalidate>
  <div ng-class="{ 'has-success':(editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine), 'has-error':(!editProduct.descriptionForm.description.$valid && !editProduct.descriptionForm.description.$pristine) || (!descriptionForm.description.$valid && descriptionForm.$submitted) }">

    <div class="entry input-group" ng-repeat="item in newDescription track by $index">
      <strong><input ng-disabled="editProduct.disabled" class="form-control" type="text" name="description" ng-model="newDescription[$index]" required></strong>
      <span class="input-group-btn">
        <a ng-click="editProduct.deleteDescription(item);" class="btn btn-remove btn-danger">
            <span class="glyphicon glyphicon-remove"></span>
        </a>
      </span>
    </div>
  </div>
  <br>
  <button ng-disabled="editProduct.disabled" class="btn btn-primary" type="submit">Save</button>
</form>

routes.js

router.put('/editProduct', function(req, res){
  var editProduct = req.body._id;
  var options = { multi: true };
  Product.findOneAndUpdate({ _id: editProduct }, { $pull: { description: req.params.description }}, options, function(err, product){
    if(err) throw err;
    if(!product){
      res.json({ success: false, message: 'Product no found' });
    } else {
      product.update(function(err){
        if(err){
          console.log(err);
        } else {
          res.json({ success: true, message: 'Description removed!'})
        }
      });
    };
  });   
});    

I also tried the following approach:

Product.findOne({'_id' : product.id}, function(err, me){
  for(var i=0; i<=me.description.length; i++){
    if (String(me.description[i])==String(uid)){
      me.description.remove(uid);
      me.save();                         
    }
  }    
});

I think, the biggest problem is that I don't how to connect this function to the button.

2
  • I would split your problem into 2-3 small ones. Commented Jul 18, 2017 at 10:11
  • It looks terrible, but I think that problem is short, maybe someone be up against removing item from array click on <button> or <a> Commented Jul 18, 2017 at 10:30

1 Answer 1

1

Please try console.log(req.params.description) Before the Mongoose update query and check if the output is indeed a valid ObjectId.

If the console output is not showing the valid uid, then the problem is in the angular code. Most probably in editProduct.deleteDescription(item) function. Check if you are making Http Request by passing the correct Description Id as the parameter. Thats probably something like item.descriptionId or item.id. Debug thoroughly.

Sign up to request clarification or add additional context in comments.

1 Comment

Everything seems works good, but I got message "Product no found"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.