Search code examples
javascriptmeteoruncaught-exceptiontypeerror

Meteor: Uncaught Error: Must be attached (delete function)


I have an error in the console every time I'm deleting an item (List) in my Meteor application. The error in the console is:

domrange.js:337 Uncaught Error: Must be attached

Here is the function, I can't understand where come from this error:

Lists.js

Meteor.methods({
   'lists.remove'(listId) {
       check(listId, String);

       const list = Lists.findOne(listId);
       if (list.owner !== this.userId) {
           throw new Meteor.Error('not-authorized');
       }
       Tasks.remove({"listId": listId});
       Lists.remove(listId);
   },

All is working properly in the application but do you know where this error can come from ?

Ps: I'm using Blaze if it can help

thanks


Solution

  • It seems I found the solution adding a Meteor.isServer or better if (!this.isSimulation) (@MasterAM solution):

        'lists.remove'(listId) {
            check(listId, String);
    
            const list = Lists.findOne(listId);
            if (list.owner !== this.userId) {
                throw new Meteor.Error('not-authorized');
            }
            if (!this.isSimulation) {
                Tasks.remove({"listId": listId});
                Lists.remove(listId);
            }
        },
    

    I edited the working code with the help of @MasterAM It'w working now! No Console error anymore.