firebase update multiple database realtime

0

I'm doing a sales system with firebase and javascript, I have products and I want to modify several of them at the same time with the following code only modifies me the first one

 for (var i = 0; i < compra.length; i++) {
        if (compra[i].value!=0){
            var key=compra[i].getAttribute("key");
            var cantidadcompra=compra[i].value;
            refproductoeditar=refproductos.child(key);
            refproductoeditar.update({
                cantidad:cantidad-cantidadcompra,
            })
        }
    }

    
asked by Adimer Paul 20.07.2018 в 15:14
source

1 answer

0

At least in the code for firebase there seems to be no problem. The sure problem is how are you getting the values of compra .

Two things may be happening

  • The compra object has only one property, and by chance it is the first of firebase .
  • The compra object has only one of its properties that matches the data saved in firebase . If this were true, you would see an error message in the console that shows you are trying to update firebase data that does not exist.
  • That only one of the properties of compra has value other than 0
  • I suggest some changes:

    for (var i = 0; i < compra.length; i++) {
            if (compra[i].value!=0){
                var key=compra[i].getAttribute("key");
                var cantidadcompra=compra[i].value;
                // Usar variables internas para evitar conflictos
                var refProductoEditar = refproductos.child(key);
                refProductoEditar.update({
                    cantidad: cantidad - cantidadcompra,
                            //  ^ de donde sale cantidad?
                })
            }
        }
    
        
    answered by 20.07.2018 в 18:28