SandeepC
SandeepC

Reputation: 101

Meteor JS Remove Single Element from Collection using _id

I am trying to remove a single document from the collection in over server side with Meteor.methods by passing _id of object ,but it is not removing object , also tried other fields in that document but no go.

I have also tried FoodCategory.remove(removeID) ; that is also not working.

File 1 - displayCategorySection.html

          <template name="categoryDisplaySection">
                <div class="row categoryDisplay">   
                    <div class="col-md-10 col-lg-10 ">
                        {{Category}}
                    </div>
                    <div class="col-md-2 col-lg-2 pull-right">
                        <i class="fa fa-minus-square"></i>
                    </div>
                </div>
                <div class="row  "> 
                    <div class="col-md-12 col-lg-12 identity">
                        {{_id}}
                    </div>          
                </div>
            </template>

In the .JS file I am passing this _id to Meteor method deleteFoodCategory

File 2 - categoryDisplaySection.js

           Template.categoryDisplaySection.events({
              'click .fa-minus-square':function(evt,tmpl) 
              {                 
                  var remove_id = tmpl.$(".identity").text();
                  alert(remove_id);
                  /*****Server side call for document remove *****/
                  Meteor.call("deleteFoodCategory",remove_id,
                  function(error,result)
                   {  alert(result);    });
              }         
           });

Server Side File 3 - FoodCategorySection.js

contain deleteFoodCategory method

            Meteor.methods({
            deleteFoodCategory: function(removeID)
            {
                    return FoodCategory.remove({
                                '_id'   : removeID
                        },
                        function(error,id)
                        {
                            if(id)  { return id;} else {    return error; }

                        });
            }
            });

Code is working correctly if I put _id like "RAEnLfomeqctuonnE" in place of variable removeID. I tried various options like '_id' or just _id without quotes , unable to figure out problem.Please take a look

Upvotes: 4

Views: 1044

Answers (2)

SandeepC
SandeepC

Reputation: 101

Answer provided by saimeunt is also working correctly as far as original problem is concern , there is need to use .trim function with remove_id variable

File 2 - categoryDisplaySection.js

    Template.categoryDisplaySection.events({
      "click .fa-minus-square": function(evt,tmpl){
       var remove_id = tmpl.$(".identity").text();

       /**This line needed to be added**/
       removeId = remove_id.trim();
       alert(removeId);

        /*****Server side call for data insert *****/
        Meteor.call("deleteFoodCategory",removeId);           
    })

but as @saimeunt has said fetching the document _id from a div text is overkill,so using this_id from now on

Upvotes: 1

saimeunt
saimeunt

Reputation: 22696

Fetching the document _id from a div text is overkill, you could use the current data context instead :

Template.categoryDisplaySection.events({
  "click .fa-minus-square": function(evt,tmpl){
    var removeId = this._id;
    alert(removeId);
    Meteor.call("deleteFoodCategory", removeId);
});

In your Meteor method, you can simply pass the _id to Collection.remove :

Meteor.methods({
  deleteFoodCategory: function(removeId){
    return FoodCategory.remove(removeId);
  }
});

Upvotes: 3

Related Questions