0

既存のデータを再開したいが、ファイアベースのクラウド関数がループ処理を行い、合計が無効になる。私の問題の解決策はありますか? はあなたfirebaseクラウド関数の関数実行中にループする

にこの私の画面撮影中

データベース構築 image、あなたがon()メソッドを使用して、データベースの参照リスナーを取り付ける

ログ image

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; 
    }); 

答えて

0

に感謝します。これは、添付のリスナーを残し:

あなたのコールバックは、データが

を変更するたびにあなたが代わりにonce()使うべき初期データと、再び のためにトリガされます:正確に一つのため

リッスンし指定されたイベントタイプのイベント、次に がリッスンを停止します。

リスナーを接続してコールバックのその場所の値を変更しているため、コードがループしています。refUser.set(newCount);

関連する問題