Stéphane R.
Stéphane R.

Reputation: 1496

Meteor : remove is not a function

I would like create global function to remove an item from my collection in Meteor.

My code :

Template.adminLayout.events({
    'click .delete': function(e) {

        var collection = $(e.target).data('collection'),
            redirect = $(e.target).data('redirect'),
            id = this._id;

        // Remove request
        collection.remove(id);

        // Redirect
        Router.go(redirect);

    }
});

With collection.remove(id), I get this error:

collection.remove is not a function

If I test with Messages (name of my collection) Messages.remove(id), it works.

Do you have idea why my code does not work?

Upvotes: 0

Views: 545

Answers (1)

Hubert OG
Hubert OG

Reputation: 19544

Your collection parameter is a string with the collection name, not the collection itself. You need the actual collection object to perform data operations. If you want to be able to access collection by name, you need to prepare a dictionary yourself. For example:

Collections = {};

Collections['Documents'] = Documents = new Mongo.Collection('documents');

Then you can use it in your event handler:

var collection = Collections[$(e.currentTarget).data('collection')];

By the way, it's good practice to use e.currentTarget instead of e.target. It always get you the element you expect, while e.target can be one of its descendants.

Upvotes: 2

Related Questions