firebase云功能函数循环执行 [英] firebase cloud function function looping in execution
问题描述
谢谢
这个在我的屏幕上拍摄
数据库建设
,
log
const functions = require (火力函数);
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。 ();
var quantity = '/ invoice_data /'+ event.params.pushIdInvoice +'/ idUser');
ref.on(value,function(snapshot){
var refUser = db.ref('/ user_data / (snapshotUser.ex()); //这个函数的作用在于: {
console.log('Ada Data');
var count = snapshotUser.val();
var newCount = quantity + count; $ b $ 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);
});
返回true;
});
=https://firebase.google.com/docs/reference/admin/node/admin.database.Reference#on =nofollow noreferrer> on()方法。这使得监听器被连接:
你的回调将被初始数据触发,而
只要数据改变就会触发
您应该改用:
$ b
侦听指定的一个事件事件类型,然后
停止监听
您的代码正在循环,因为您附加了一个侦听器 refUser .on(...)
,然后改变回调中该位置的值: refUser.set(newCount);
I want to make a resume of the existing data but Firebase cloud function does the looping so that the summation I made is not valid. is there a solution to my problem? thank you
this in my screen shoot
database construction ,
log
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;
});
You are attaching database reference listeners using the on() method. This leaves the listener attached:
Your callback will be triggered for the initial data and again whenever the data changes
You should instead use once():
Listens for exactly one event of the specified event type, and then stops listening
Your code is looping because you attach a listener refUser.on(...)
and then are changing the value at that location in the callback: refUser.set(newCount);
这篇关于firebase云功能函数循环执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!