Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!