简体   繁体   中英

Calculate total from Firestore database

trying to get total price from three fields in fire store database

cashamout: '500000' cashamout: '46000' cashamout: "58000"

i am using the code below..but instead it gave me a result of 05000004600058000 instead of summing the value. Please what am i doing wrong?

 var totalCash = 0;
    this.adb.collection('cash').get().toPromise().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      console.log('VALUESSS',doc.data())
        totalCash = totalCash + (doc.data().cashamout);
    });
    console.log('TOTALCLCLSLS',totalCash);
});

Your numbers are not actually numbers at all. They are strings. You can tell because they will have quotes around them when viewing in the console.

When you "add" two strings, they are concatenated just like in the output that you showed.

If you want to store a number in Firestore, you should make sure that the input data is actually a JavaScript number type before you write the document , and not a string as they are now. This will allow them to sort and participate in math correctly.

i just converted to integer and it worked now

Number(doc.data().cashamout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM