简体   繁体   English

从 Firestore 数据库计算总数

[英]Calculate total from Firestore database

trying to get total price from three fields in fire store database试图从消防商店数据库中的三个字段中获取总价

cashamout: '500000' cashamout: '46000' cashamout: "58000"现金:“500000” 现金:“46000” 现金:“58000”

i am using the code below..but instead it gave me a result of 05000004600058000 instead of summing the value.我正在使用下面的代码..但它给了我一个 05000004600058000 的结果,而不是对值求和。 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.当您“添加”两个字符串时,它们会像您展示的 output 一样连接起来。

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.如果要在 Firestore 中存储数字,则应在编写文档之前确保输入数据实际上是 JavaScript 数字类型,而不是像现在这样的字符串。 This will allow them to sort and participate in math correctly.这将使他们能够正确地排序和参与数学。

i just converted to integer and it worked now我刚刚转换为 integer 并且现在可以使用

Number(doc.data().cashamout);数字(doc.data().cashamout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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