gpt4 book ai didi

javascript - Firebase 中的递归错误太多

转载 作者:行者123 更新时间:2023-11-29 21:11:24 25 4
gpt4 key购买 nike

我在我的全局范围内有一个函数,用于监听 FB ref 上的值。

function updateCredits(userID){

var userRef = database.ref('users/' + userID);
userRef.on('value', function(snapshot){
var userCredits = snapshot.val().credits;
console.log(userCredits);
console.log('account: ' + userID + " has " + userCredits + " credits");
var updatedCredits = userCredits + ANTE;
userRef.update({credits: updatedCredits});
console.log("now it has " + updatedCredits + ANTE);
});
}

articlesRef.on('child_added', function(data) {
var upVoteRef = database.ref('articles/' + data.key + '/upVotes/');
try {
var upVotersRef = database.ref('articles/' + data.key + '/upVoters/');
upVoteRef.on('value', function(childData){
upVotersRef.on('value', function(voterData){
var voterKeys = Object.keys(voterData.val());
for (var i=0; i<voterKeys.length; i++){
upVotersRef.child(voterKeys[i]).on('value', function(voterID){
console.log("trying with voter ID " + voterID.val());
updateCredits(voterID.val());
});
}
});
});
} catch(err){

console.log(err.message);

}

var downVoteRef = database.ref('articles/' + data.key + '/downVotes/');
downVoteRef.on('value', function(childData){
console.log(childData.val());

});


});

不知何故,当我加载页面时,这个函数显然被调用了数百或数千次。我将其记录到控制台:

account: HMHZ8Ga3UGhc2wSczHDuFL82FB02 has 1328.6999999997329 credits  app.js:25:9
1328.6999999997329 app.js:24:9
1328.8999999997327 app.js:24:9
account: HMHZ8Ga3UGhc2wSczHDuFL82FB02 has 1328.8999999997327 credits app.js:25:9
1328.8999999997327 app.js:24:9
account: HMHZ8Ga3UGhc2wSczHDuFL82FB02 has 1328.8999999997327 credits app.js:25:9
1328.9999999997326 app.js:24:9
account: HMHZ8Ga3UGhc2wSczHDuFL82FB02 has 1328.9999999997326 credits app.js:25:9
1328.9999999997326 app.js:24:9
account: HMHZ8Ga3UGhc2wSczHDuFL82FB02 has 1328.9999999997326 credits ...

(如您所见,出于某种原因,同一个帐户被重复授予积分。我希望它仅在 upVote 时获得积分);

它继续......

now it has 1330.09999999973160.1  app.js:28:9
now it has 1329.99999999973170.1 app.js:28:9
now it has 1329.89999999973180.1 app.js:28:9
now it has 1329.79999999973190.1 app.js:28:9
now it has 1329.6999999997320.1 app.js:28:9

...最后,问题:

too much recursion[Learn More]  firebase.js:293:781

我猜 updateCredits 函数正在触发 upVoteRef 监听的事件,upVoteRef 依次调用 updateCredits 函数并变为循环。我不知道如何避免这种情况。有什么想法吗?

最佳答案

解决方案

你可以做

userRef.once('value').then(function(snapshot){

代替

userRef.on('value', function(snapshot){  

解释

on('value') 的作用是触发一次以获取该路径上的所有数据,然后在每次该路径上的数据发生变化 时再次触发。

由于您在回调中更新同一路径上的数据

userRef.update({credits: updatedCredits});

该路径上的数据发生变化,原始事件(usersRef 上的 .value)再次触发,这又触发您的回调,您再次更新值 which .... 等等。这是一个无限循环,会导致您的错误。

为避免这种情况,您可以根据要实现的目标执行几项操作。如果你只想在有人跑的时候加 ANTE

updateCredits(voterID.val()); 

然后您只想读取 userRef 中的数据一次,而不主动观察该路径。因此,.once 而不是 .on

参见 Read data once - Firebase Documentation

关于javascript - Firebase 中的递归错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com