我的全局作用域中有一个函数,用于监听 FB 引用上的值。
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 监听的事件,该事件又调用 updateCredits 函数并变成循环。我不知道如何避免这种情况。有什么想法吗?
最佳答案
解决方案
你可以这样做
userRef.once('value').then(function(snapshot){
而不是
userRef.on('value', function(snapshot){
说明
on('value') 的作用是,它会触发一次以获取该路径上的所有数据,然后每次该路径上的数据发生变化时再次触发。
由于您在回调中更新了同一路径上的数据
userRef.update({credits: updatedCredits});
该路径上的数据发生变化,原始事件(usersRef 上的 .value)再次触发,这反过来又触发您的回调,您再次更新值......等等。这是一个无限循环,会导致您的错误。
为了避免这种情况,您可以根据您想要实现的目标执行多种操作。如果您只想在有人运行时添加 ANTE
updateCredits(voterID.val());
那么您只想读取 userRef 处的数据一次,而不是主动观察该路径。因此,.once 而不是 .on
关于javascript - Firebase 中的递归错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699446/