How to stop events while method finishes


#1

Hello, I have this code on my client to update a value when the client presses on a div element with the class ‘contadorOrdenMenos’, the problem is that I don’t want the method “sumarProducto” called if the value ‘this.cantidad’ is less than or 1 and everything works great if the user clicks slowly on the div with the ‘contadorOrdenMenos’, but if the user clics repeatedly (when the value is greater than one) on it, then the condition is not met. I think this problem is because the ‘this.canitdad’ value is not yet updated on the DB and the function can still be used

'click .contadorOrdenMenos': function () {
      if(this.cantidad!=1){
        Meteor.call("sumarProducto", this.productos._id, -1, this.cantidad, function(err,res){
          if(err){
            swal("Error", "No pudimos quitar el producto al carrito, vuelve a intentarlo.", "warning");
          }
        });
      }
    }

Another thing I tried was making this validation on the server using this method, but because the value I want to check is inside an array inside the db I couldn’t find out how to get it’s value:

sumarProducto: function (idProducto, cantidadSuma) {
        console.log(CarritoUsuario.findOne({idUsuario: Meteor.userId(),"productos.id":idProducto},{"productos.$.cantidad": { $gt: 1 } }));
        if(CarritoUsuario.findOne({idUsuario: Meteor.userId(),"productos.id":idProducto},{ "productos.$.cantidad": { $gte: 1 } })){
            CarritoUsuario.update({idUsuario: Meteor.userId(),"productos.id":idProducto},{$inc: {"productos.$.cantidad": cantidadSuma}});
        }
    }

Thank you very much in advance!


#2

About your first question, I see no reason why the behavior would change if user clicks repeatedly. Maybe you have a double-click handler somewhere? or this.cantidad is updated reactively when you call sumarProducto?

About your second question, I don’t understand which value you want to check in the array. If you want to increment, in the array, any value that is >= 1, I don’t think there is a way to do this with one query. You will have to fetch the array, update it, and write it back.


#3
'click .contadorOrdenMenos': function (event) {
    if(this.cantidad!=1){
        event.currentTarget.style.pointerEvents = 'none';
        Meteor.call("sumarProducto", this.productos._id, -1, this.cantidad, function(err,res){
            event.currentTarget.style.pointerEvents = 'all';
            <...>
        });
    }
}

For the first question. And second isn’t clear.


#4

Thank you very much @mrzafod this worked perfectyl!


#5

Your second question I gues should be: for the query

if CarritoUsuario.find({idUsuario: Meteor.userId(), "productos.id": idProducto, "productos.cantidad": { $gte: 1 }).count()