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!