【问题标题】:How to update childSnapShot value in firebase web如何在 Firebase Web 中更新 childSnapShot 值
【发布时间】:2021-05-11 02:42:43
【问题描述】:

我在 firebase 的产品表上有两种产品。这里是:

现在我想在用户采取行动时更新 product_quantity。但是当用户选择两个或多个产品并输入选择的产品数量时,只有最后一个产品数量被替换或更新。产品选择视图如下所示: 第二个产品选择如下所示:

这里是javascript的代码:

firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        var uid = user.uid;
        for (var i = 0; i < itemCnt; i++) {
            var uidValueInv = $(".item-wrapper").eq(i).find(".uidVal").val();
            var productNameInv = $(".item-wrapper").eq(i).find(".productName").val();
            var amountInv = parseFloat($(".item-wrapper").eq(i).find(".amount").val());
            var sellingPriceInv = parseFloat($(".item-wrapper").eq(i).find(".sellPrice").val());
            var buyingPriceInv = parseFloat($(".item-wrapper").eq(i).find(".buyPrice").val());
            var quantityInv = parseFloat($(".item-wrapper").eq(i).find(".quantity").val()) || 0;
            var quantityCount = amountInv - quantityInv;
                                console.log(quantityCount)
            var query = firebase.database().ref("Products/").child(uid).
                        orderByChild("uid").equalTo(uidValueInv);
            query.once("child_added", function(snapshot) {
                  snapshot.ref.update({
                      product_quantity: quantityCount.toString()
                  })
            })
    }
})

我在这里只上传了 firebase 代码,而不是整个 javascript 如何计算 product_quantity 和其他东西,因为这些东西完美运行我检查了完美获得 product_quantity 的控制台。我想我只需要更改 firebase 更新代码。

【问题讨论】:

    标签: javascript firebase web firebase-realtime-database


    【解决方案1】:

    我更改以下代码:

    firebase.database().ref('Products/').child(uid).once('value').then(function (snapshot) 
       {
           for (var i = 0; i < itemCnt; i++) {
               var uidValueInv = $(".item-wrapper").eq(i).find(".uidVal").val();
               console.log(uidValueInv);
               var amountInv = parseFloat($(".item-wrapper").eq(i).find(".amount").val());
               var quantityInv = parseFloat($(".item-wrapper").eq(i).find(".quantity")
                                 .val()) || 0;
               var quantityCount = amountInv - quantityInv;
               console.log(quantityCount)
               snapshot.forEach(function (childSnapshot) {
                   var uid_value = childSnapshot.child('uid').val();
                   console.log(uid_value)
                       if (uid_value === uidValueInv) {
                           childSnapshot.ref.update({
                               product_quantity: quantityCount.toString()
                           });
                       }
                })
            }
       })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多