【问题标题】:firebase cloud function function looping in executionfirebase云函数函数在执行中循环
【发布时间】:2017-08-03 05:38:19
【问题描述】:

我想恢复现有数据,但 Firebase 云函数会执行循环,因此我所做的总和无效。我的问题有解决方案吗? 谢谢

这是我的屏幕截图

数据库建设 ,

日志

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
var db = admin.database();
exports.makePurchaseSummaryAdd = functions.database.ref('/invoice_data/{pushIdInvoice}/item/{pushIdItem}')
    .onCreate(event => {
        var name = event.data.child('itemName').val();
        var quantity = event.data.child('quantity').val();

        var ref = db.ref('/invoice_data/' + event.params.pushIdInvoice + '/idUser');
        ref.on("value", function(snapshot) {
            var refUser = db.ref('/user_data/' + snapshot.val() + '/purchaseSummary/' + name.toUpperCase());
            refUser.on("value", function(snapshotUser) {
                if (snapshotUser.exists()){
                    console.log('Ada Data');
                    var count = snapshotUser.val();
                    var newCount = quantity + count;
                    refUser.set(newCount);
                    console.log('create', count, newCount, name.toUpperCase());
                }
                else {
                    console.log('Tidak Ada Data');
                }
            }, function (errorObject) {
              console.log("The read failed: " + errorObject.code);
            });
        }, function (errorObject) {
          console.log("The read failed: " + errorObject.code);
        });
        return true;
    });

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您正在使用on() 方法附加数据库引用侦听器。这使监听器保持连接:

    您的回调将触发初始数据并再次触发 每当数据发生变化时

    你应该改用once():

    只监听一个指定事件类型的事件,然后 停止听

    您的代码正在循环,因为您附加了一个侦听器 refUser.on(...),然后在回调中更改该位置的值:refUser.set(newCount);

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2018-01-21
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多