Method called once, executed twice

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 ?

1 Like

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.

1 Like

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?

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?

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);
    }
});

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

The issue is better described here.

Isn’t Meteor.user reactive?

@Steve, see my answer on the meteor issue tracked

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