这是我的代码,它运行多个循环并永远完成。
ratingChanged(altHash, newRating, event) {
var alt_md5hash = altHash;
var userRateID = this.state.userID;
// console.log("RATING HASH: ", altHash);
// console.log("RATING: ", newRating);
// console.log("userRateID: ", userRateID);
// Update/Add unique note ratings by user
var newRateKey = firebase.database().ref('notes/' + alt_md5hash).push().key;
var updates = {};
updates['notes/' + alt_md5hash + "/RatingsByUsers/" + userRateID] = newRating;
firebase.database().ref().update(updates);
// Count number of ratings and average
var numRatings = 0;
var sumRatings = 0;
var notesDBRate = firebase.database().ref('notes/' + alt_md5hash + "/RatingsByUsers");
notesDBRate.on('value', function(noteSnapshot) {
noteSnapshot.forEach(function(childSnapshot) {
numRatings += 1;
sumRatings += childSnapshot.val();
console.log("numRatings1: ", numRatings);
console.log("sumRatings2: ", sumRatings);
}.bind(this));
var averageRating = sumRatings / parseFloat(numRatings); // force float
console.log("==========AVG RATE: ", averageRating);
var updateRateNum = {};
updateRateNum['notes/' + alt_md5hash + "/uniqueRatings"] = numRatings;
updateRateNum['notes/' + alt_md5hash + "/starRating"] = averageRating;
console.log("UPDATED!!!!!!!!!!!!!!!!!!!!!");
firebase.database().ref().update(updateRateNum);
}.bind(this));
//Add download number to reference of note
var ownerID = "";
var notesDB = firebase.database().ref('notes/' + alt_md5hash);
notesDB.on('value', function(noteSnapshot) {
ownerID = noteSnapshot.val().ownerID;
var rateSum = noteSnapshot.val().uniqueRatings;
var rateAvg = noteSnapshot.val().starRating;
console.log("ownerID REAL: ", ownerID);
var updateOwnerRating = {};
updateOwnerRating['userNotes/' + ownerID + "/" + alt_md5hash + "/uniqueRatings"] = rateSum;
updateOwnerRating['userNotes/' + ownerID + "/" + alt_md5hash + "/starRating"] = rateAvg;
firebase.database().ref().update(updateOwnerRating);
}.bind(this));
//update user's total downloads
var avgUserNoteRating = 0;
var avgUserNoteRatingCount = 0;
console.log("OWNERID READ 2nd: ", ownerID);
var ownerDB = firebase.database().ref('userNotes/' + ownerID);
ownerDB.on('value', function(ownerSnapshot) {
ownerSnapshot.forEach(function(childSnapshot) {
avgUserNoteRating += childSnapshot.val().starRating;
avgUserNoteRatingCount += 1;
console.log("inner1: ", avgUserNoteRating);
console.log("inner2: ", avgUserNoteRatingCount);
}.bind(this));
// update USER total rating
var allAroundRating = avgUserNoteRating/parseFloat(avgUserNoteRatingCount) || 0;
console.log("==========allAroundRating: ", allAroundRating);
console.log("UPDATING SECOND :))))))))))))))))");
var updateTotalUserRatings = {};
updateTotalUserRatings['users/' + ownerID + "/holisticUserNoteRating"] = allAroundRating;
firebase.database().ref().update(updateTotalUserRatings);
}.bind(this));
}
这是我的console.log供人们关注。
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 618.4470238095238
Notefeed.js:221 inner2: 201
Notefeed.js:220 inner1: 623.4470238095238
Notefeed.js:221 inner2: 202
Notefeed.js:220 inner1: 625.1970238095238
Notefeed.js:221 inner2: 203
Notefeed.js:220 inner1: 625.6970238095238
Notefeed.js:221 inner2: 204
Notefeed.js:220 inner1: 628.6970238095238
Notefeed.js:221 inner2: 205
Notefeed.js:225 ==========allAroundRating: 3.06681475029036
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 529.1053571428572
Notefeed.js:221 inner2: 171
Notefeed.js:220 inner1: 534.1053571428572
Notefeed.js:221 inner2: 172
Notefeed.js:220 inner1: 535.8553571428572
Notefeed.js:221 inner2: 173
Notefeed.js:220 inner1: 536.3553571428572
Notefeed.js:221 inner2: 174
Notefeed.js:220 inner1: 539.3553571428572
Notefeed.js:221 inner2: 175
Notefeed.js:225 ==========allAroundRating: 3.0820306122448984
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 360.8220238095238
Notefeed.js:221 inner2: 116
Notefeed.js:220 inner1: 365.8220238095238
Notefeed.js:221 inner2: 117
Notefeed.js:220 inner1: 367.5720238095238
Notefeed.js:221 inner2: 118
Notefeed.js:220 inner1: 368.0720238095238
Notefeed.js:221 inner2: 119
Notefeed.js:220 inner1: 371.0720238095238
Notefeed.js:221 inner2: 120
Notefeed.js:225 ==========allAroundRating: 3.092266865079365
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 236.31755952380954
Notefeed.js:221 inner2: 76
Notefeed.js:220 inner1: 241.31755952380954
Notefeed.js:221 inner2: 77
Notefeed.js:220 inner1: 243.06755952380954
Notefeed.js:221 inner2: 78
Notefeed.js:220 inner1: 243.56755952380954
Notefeed.js:221 inner2: 79
Notefeed.js:220 inner1: 246.56755952380954
Notefeed.js:221 inner2: 80
Notefeed.js:225 ==========allAroundRating: 3.0820944940476194
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 95.50992063492063
Notefeed.js:221 inner2: 31
Notefeed.js:220 inner1: 100.50992063492063
Notefeed.js:221 inner2: 32
Notefeed.js:220 inner1: 102.25992063492063
Notefeed.js:221 inner2: 33
Notefeed.js:220 inner1: 102.75992063492063
Notefeed.js:221 inner2: 34
Notefeed.js:220 inner1: 105.75992063492063
Notefeed.js:221 inner2: 35
Notefeed.js:225 ==========allAroundRating: 3.0217120181405894
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
(DELETED A CHUNK SO FITS INSIDE STACKOVERFLOW CHARACTER LIMIT)
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:203 ownerID REAL: vbHQ2aiIekap6AqLGiwEHVRprhV2
Notefeed.js:220 inner1: 905.1553571428574
Notefeed.js:221 inner2: 296
Notefeed.js:220 inner1: 910.1553571428574
Notefeed.js:221 inner2: 297
Notefeed.js:220 inner1: 912.0839285714288
Notefeed.js:221 inner2: 298
Notefeed.js:220 inner1: 912.5839285714288
Notefeed.js:221 inner2: 299
Notefeed.js:220 inner1: 915.5839285714288
Notefeed.js:221 inner2: 300
Notefeed.js:225 ==========allAroundRating: 3.0519464285714295
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 874.4053571428574
Notefeed.js:221 inner2: 286
Notefeed.js:220 inner1: 879.4053571428574
Notefeed.js:221 inner2: 287
Notefeed.js:220 inner1: 881.3339285714288
Notefeed.js:221 inner2: 288
Notefeed.js:220 inner1: 881.8339285714288
Notefeed.js:221 inner2: 289
Notefeed.js:220 inner1: 884.8339285714288
Notefeed.js:221 inner2: 290
Notefeed.js:225 ==========allAroundRating: 3.051151477832513
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 770.4886904761905
Notefeed.js:221 inner2: 251
Notefeed.js:220 inner1: 775.4886904761905
Notefeed.js:221 inner2: 252
Notefeed.js:220 inner1: 777.417261904762
Notefeed.js:221 inner2: 253
Notefeed.js:220 inner1: 777.917261904762
Notefeed.js:221 inner2: 254
Notefeed.js:220 inner1: 780.917261904762
Notefeed.js:221 inner2: 255
Notefeed.js:225 ==========allAroundRating: 3.062420634920635
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 709.1345238095239
Notefeed.js:221 inner2: 231
Notefeed.js:220 inner1: 714.1345238095239
Notefeed.js:221 inner2: 232
Notefeed.js:220 inner1: 716.0630952380953
Notefeed.js:221 inner2: 233
Notefeed.js:220 inner1: 716.5630952380953
Notefeed.js:221 inner2: 234
Notefeed.js:220 inner1: 719.5630952380953
Notefeed.js:221 inner2: 235
Notefeed.js:225 ==========allAroundRating: 3.061970618034448
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 633.4470238095238
Notefeed.js:221 inner2: 206
Notefeed.js:220 inner1: 638.4470238095238
Notefeed.js:221 inner2: 207
Notefeed.js:220 inner1: 640.3755952380952
Notefeed.js:221 inner2: 208
Notefeed.js:220 inner1: 640.8755952380952
Notefeed.js:221 inner2: 209
Notefeed.js:220 inner1: 643.8755952380952
Notefeed.js:221 inner2: 210
Notefeed.js:225 ==========allAroundRating: 3.0660742630385487
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 544.1053571428572
Notefeed.js:221 inner2: 176
Notefeed.js:220 inner1: 549.1053571428572
Notefeed.js:221 inner2: 177
Notefeed.js:220 inner1: 551.0339285714286
Notefeed.js:221 inner2: 178
Notefeed.js:220 inner1: 551.5339285714286
Notefeed.js:221 inner2: 179
Notefeed.js:220 inner1: 554.5339285714286
Notefeed.js:221 inner2: 180
Notefeed.js:225 ==========allAroundRating: 3.080744047619048
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 375.8220238095238
Notefeed.js:221 inner2: 121
Notefeed.js:220 inner1: 380.8220238095238
Notefeed.js:221 inner2: 122
Notefeed.js:220 inner1: 382.75059523809523
Notefeed.js:221 inner2: 123
Notefeed.js:220 inner1: 383.25059523809523
Notefeed.js:221 inner2: 124
Notefeed.js:220 inner1: 386.25059523809523
Notefeed.js:221 inner2: 125
Notefeed.js:225 ==========allAroundRating: 3.090004761904762
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 251.31755952380954
Notefeed.js:221 inner2: 81
Notefeed.js:220 inner1: 256.31755952380956
Notefeed.js:221 inner2: 82
Notefeed.js:220 inner1: 258.246130952381
Notefeed.js:221 inner2: 83
Notefeed.js:220 inner1: 258.746130952381
Notefeed.js:221 inner2: 84
Notefeed.js:220 inner1: 261.746130952381
Notefeed.js:221 inner2: 85
Notefeed.js:225 ==========allAroundRating: 3.0793662464986
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 110.50992063492063
Notefeed.js:221 inner2: 36
Notefeed.js:220 inner1: 115.50992063492063
Notefeed.js:221 inner2: 37
Notefeed.js:220 inner1: 117.43849206349206
Notefeed.js:221 inner2: 38
Notefeed.js:220 inner1: 117.93849206349206
Notefeed.js:221 inner2: 39
Notefeed.js:220 inner1: 120.93849206349206
Notefeed.js:221 inner2: 40
Notefeed.js:225 ==========allAroundRating: 3.0234623015873017
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:225 ==========allAroundRating: 3.059814484126984
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 845.9964285714286
Notefeed.js:221 inner2: 276
Notefeed.js:220 inner1: 850.9964285714286
Notefeed.js:221 inner2: 277
Notefeed.js:220 inner1: 853.0901785714286
Notefeed.js:221 inner2: 278
Notefeed.js:220 inner1: 853.5901785714286
Notefeed.js:221 inner2: 279
Notefeed.js:220 inner1: 856.5901785714286
Notefeed.js:221 inner2: 280
Notefeed.js:225 ==========allAroundRating: 3.059250637755102
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 770.3089285714285
Notefeed.js:221 inner2: 251
Notefeed.js:220 inner1: 775.3089285714285
Notefeed.js:221 inner2: 252
Notefeed.js:220 inner1: 777.4026785714285
Notefeed.js:221 inner2: 253
Notefeed.js:220 inner1: 777.9026785714285
Notefeed.js:221 inner2: 254
Notefeed.js:220 inner1: 780.9026785714285
Notefeed.js:221 inner2: 255
Notefeed.js:225 ==========allAroundRating: 3.062363445378151
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 680.9672619047619
Notefeed.js:221 inner2: 221
Notefeed.js:220 inner1: 685.9672619047619
Notefeed.js:221 inner2: 222
Notefeed.js:220 inner1: 688.0610119047619
Notefeed.js:221 inner2: 223
Notefeed.js:220 inner1: 688.5610119047619
Notefeed.js:221 inner2: 224
Notefeed.js:220 inner1: 691.5610119047619
Notefeed.js:221 inner2: 225
Notefeed.js:225 ==========allAroundRating: 3.0736044973544976
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 512.6839285714286
Notefeed.js:221 inner2: 166
Notefeed.js:220 inner1: 517.6839285714286
Notefeed.js:221 inner2: 167
Notefeed.js:220 inner1: 519.7776785714286
Notefeed.js:221 inner2: 168
Notefeed.js:220 inner1: 520.2776785714286
Notefeed.js:221 inner2: 169
Notefeed.js:220 inner1: 523.2776785714286
Notefeed.js:221 inner2: 170
Notefeed.js:225 ==========allAroundRating: 3.078103991596639
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 388.17946428571435
Notefeed.js:221 inner2: 126
Notefeed.js:220 inner1: 393.17946428571435
Notefeed.js:221 inner2: 127
Notefeed.js:220 inner1: 395.27321428571435
Notefeed.js:221 inner2: 128
Notefeed.js:220 inner1: 395.77321428571435
Notefeed.js:221 inner2: 129
Notefeed.js:220 inner1: 398.77321428571435
Notefeed.js:221 inner2: 130
Notefeed.js:225 ==========allAroundRating: 3.067486263736264
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
Notefeed.js:220 inner1: 247.37182539682541
Notefeed.js:221 inner2: 81
Notefeed.js:220 inner1: 252.37182539682541
Notefeed.js:221 inner2: 82
Notefeed.js:220 inner1: 254.46557539682541
Notefeed.js:221 inner2: 83
Notefeed.js:220 inner1: 254.96557539682541
Notefeed.js:221 inner2: 84
Notefeed.js:220 inner1: 257.96557539682544
Notefeed.js:221 inner2: 85
Notefeed.js:225 ==========allAroundRating: 3.0348891223155934
Notefeed.js:226 UPDATING SECOND :))))))))))))))))
似乎激活此ratingFunction
(通过点击星号),代码(尤其是“ON值”部分)正在触发,读取和更新,但由于它正在更新,因此它是再读一遍。
不知道问题出在哪里,所以看到错误的人请告诉我。
答案 0 :(得分:1)
如果您正在使用您收到更新值的相同代码更新节点,这听起来像是我预期的行为。
如果您尝试根据当前值更新节点,则需要使用事务。事务将当前值的读取和新值的写入组合到单个操作中。见https://firebase.google.com/docs/database/web/read-and-write#save_data_as_transactions