Method called once, executed twice


#1

I have a client-server method that looks like this:

Meteor.methods({
  removeUser: function () {
    if (this.userId)
      Meteor.users.remove(this.userId);
  }
});

When I call this method from the client, it is executed once on client and twice on server (with this.userId to null the second time).
I have read things about Meteor methods not being idempotent and possibly called several times. But why systematically in this case ?


#2

Just so we know it isn’t an issue with how you’re calling the method, when you run it from browser console: Meteor.call('removeUser') you see it happen twice on the server? I am guessing in testing you’ve put something inside the method to log that its being run more than once.

Meteor methods in my experience are not idempotent unless you explicitly design them to be.
I use methods all the time and have never had the experience you’re describing.


#3

May be you call this method inside a computation context? I mean:

Tracker.autorun ->
    Meteor.userId()
    Meteor.call('someMethod')

Or your method call stub isn’t finished and on the next server call it is called from methods queue once again?


#4

The method is called from an event handler (‘click button’), with no autorun. I get the same behavior when calling it from the console.

It has to relate to what I am doing in the method: deleting the currently logged-in user.

After further investigation, I noticed this problem occurs only if deleting a user whose accounts has been merged with accounts-meld. Deleting a “normal” user causes no issue (method called once).

@splendido, any idea before I investigate deeper?


#5

I suppose you have to find full call stack for the method. You can debug try to add a breakpoint at the method or try to trace the stack

Meteor.methods({
    removeUser: function () {
        try {
            throw new Error();
        } catch (e) {
            console.log(e.stack);
        }
        if (this.userId)
            Meteor.users.remove(this.userId);
    }
});

#6

It does not relate to accounts-meld, I could finally reproduce it on a “normal” account.

The issue is better described here.


#7

Isn’t Meteor.user reactive?


#8

@Steve, see my answer on the meteor issue tracked

@jamgold: see the docs about this.userId: no mentions about reactivity on server side methods