Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Firebase JS how do I collect the children of the product that each child has a quantity?

I have products that evrey product have children and evrey child have children and i want to collect the sum for evrey product enter image description here

my code is:

var firebaseProductsCollection = database.ref().child('finalVerifoneProducts');
firebaseProductsCollection.on('value', function(products) {
  products.forEach(function(firebaseProductReference) {
    code = firebaseProductReference.key;
    products.forEach(function(firebaseProductReference) {
      var code = firebaseProductReference.key;
      var firebaseGetBarcodes = database.ref().child('finalVerifoneProducts').child(code);
      firebaseGetBarcodes.on('value', function(productsBarcodes) {
        productsBarcodes.forEach(function(firebaseProductReference2) {
              var product = firebaseProductReference2.val();
              if (!productArray.includes(product.code)) {
                // we have new product 
                console.log(product.code + " : " + totalSum);
                productArray.push(product.code);
                totalSum = product.sum;
              } else {
                totalSum = parseInt(totalSum) + parseInt(product.sum);
              } // close the else 

But the quantity of the first product is always 0 and the rest of the products gets wrong

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you load a path from the database, all data under that path is already included in the snapshot you get back. So there's no need to separate load the nested child data.

Knowing that, you can significantly simplify your code to:

const firebaseProductsCollection = database.ref().child('finalVerifoneProducts');
firebaseProductsCollection.on('value', function(products) {
  let totalSum = 0; // 👈
  products.forEach(function(productSnapshot) {
    code = productSnapshot.key;
    productSnapshot.forEach(function(productChildSnapshot) {
      var product = productChildSnapshot.val();
      totalSum += product.sum;
    });
  });
  console.log(totalSum); // 👈
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda