2

Always checked :

Hello,

I have discovered in my database the Meteor Method have been executed several times from click or keypress.

I don't manage to trigger the bug.

Here my code :

class Answering extends Component {
...

validAnswer() {
  ...

  validAnswer.call({
    ...
  });
}
...
render() {
    return (
...
<div
  id="Answering-button-next"
  role="button"
  onClick={() => { this.validAnswer(); }}
  onKeyPress={(e) => { if (e.key === 'Enter') this.validAnswer(); }}
  tabIndex="0"
>OK
</div>

How can fix this bug ?

Thank you

DonFabiolas
  • 439
  • 1
  • 7
  • 17

2 Answers2

1

you should be able to use a class property as a local class variable. use that to check if the current request is processing or not. if it's processing, early return so the method isn't called. if it isn't processing, toggle it as processing, and set it back to not processing in the method callback when the server request is complete. this will prevent a duplicate from happening until the callback has ran (indicating a successful server method response).

class Answering extends Component {
    isProcessing: false,

    validAnswer() {
        if (this.isProcessing) return;
        this.isProcessing = true;
        validAnswer.call({}, () => {
            this.isProcessing = false;
        });
    }

    render() {
        return (
            <div
                id="Answering-button-next"
                role="button"
                onClick={() => { this.validAnswer(); }}
                onKeyPress={(e) => {
                    if (e.key === 'Enter') this.validAnswer();
                }}
                tabIndex="0"
            >
                OK
            </div>
        );
    }
}
Mark Shust at M.academy
  • 6,300
  • 4
  • 32
  • 50
  • Thanks but with your solution, I need to do this in all my programs ? Any idea about origin of my bug ? – DonFabiolas May 21 '18 at 09:25
  • 1
    Yes. Your bug is that the user is able to process the method twice, this prevents it. You can create a higher order component which contains this logic, then use this component anywhere you need to call a method. – Mark Shust at M.academy May 22 '18 at 11:56
0

I found the bug,

The bug comes from the disconnection of server.

When you Server is disconnected and the user clicks on a button that calls a method ... so ... nothing happens ... and user continue to click on the button and Meteor keeps in Memory all actions.

Then, when the Server comes back and turns on, so , all client requests are sent to server.

Thus, the method is called several times.

Hope, I was clear ;-)

DonFabiolas
  • 439
  • 1
  • 7
  • 17
  • 1
    I'm thinking you should still accept my answer as the valid answer, because long-running tasks on the server act just like a disconnection. you need client-side validation to check if the server method call has completed, otherwise you will also get the duplicated call. also, double clicking on your button will also cause the method to be called twice -- my suggested client-side validation checking fixes that. – Mark Shust at M.academy Jun 04 '18 at 16:33