Method executed twice, maybe on client

When the user calls insert method from the client, it is executed twice. At first, I suspected user called it once on client and it executed twice on server(low internet connection) but after I make some adjustments to DateTime by replacing hour/minute/second/millisecond with the current time before insert, the two duplicated data on Database is different by milliseconds. If it is only called once on client and executed twice on server the DateTime(reqDate, refDate) of the two records should be the same.

Screen Shot 2023-01-11 at 10.40.36 AM

And here is my function to replace DateTime with the current time:

const wrapCurrentTime = (date) => {
  date = moment(date)
  const currentDate = moment()

  date.hour(currentDate.hour())
  date.minute(currentDate.minute())
  date.second(currentDate.second())
  date.millisecond(currentDate.millisecond())

  return date
}

Client method:

 submit() {
  this.loading = true
  const input= this.form

  input.refDate = wrapCurrentTime(
    moment(input.refDate, 'DD/MM/YYYY').toDate()
  )
  input.reqDate = wrapCurrentTime(
    moment(input.reqDate, 'DD/MM/YYYY').toDate()
  )

  Meteor.apply('insertData', [input], { noRetry: true }, (error) => {
    if (error) {
      Notify.error({ message: error })
      this.loading = false
      return
    }

    Msg.success()
    this.reset()
    this.loading = false
  })
}

It didn’t happen that often and I haven’t found the real cause of this issue, Is there a way to prevent this?

Search google how to debounce javascript client events

So you think it is client-side issue? I’ll add debounce and see if it would work.
Thanks

things to double check:
a) if Meteor.apply is called only once
b) if submit() is called only once

simply insert some console.log() in each to see how many times each is being executed.

if both execute once, then do the same in “insertData” method and double check that method. If any of those repeat twice - some issues in the GUI that makes a submit() call.

1 Like

The data indicated that the wrapCurrentTime() is called twice in the client. There is no other reason why there will be two sets of data with different “timestamp” generated. If the method is called twice only in the server, then the data will be the same (which is not). Therefore, the issue is client-side

1 Like

It’s hard to reproduce the issue, It only happens on production and for some reason, a user is able to call the method twice in less than a second.
I’ve tried to rapidly click submit button but it only calls once a time (because it displays a loading screen while the method is still running)

Thank you for your explanation it makes me clear now.
BTW, I’ve added a rate-limiting rule on insertData to allow one attempt every 2 seconds for each connection