function add(){
firebase.database().ref("user/l6xetRv3ddhjlerJm10d3aYkF3w1" ).update({
points: "2000"
})
}
我正在使用上面的代碼更新指定的用戶點進行測驗,但我想更新當前登錄用戶的點。我嘗試在函式 onAuthStateChanged 塊上執行此操作,但沒有成功。有什么建議我應該怎么做?
是否可以使用當前更新點更新以前的點?例如:- 用戶之前的積分是 2000,更新功能更新 2000 積分,但我希望它更新之前的積分 當前積分,即 goona 更新(2000 2000)。
uj5u.com熱心網友回復:
要寫入當前登錄用戶的積分:
const uid = firebase.auth().currentUser.uid;
firebase.database().ref("user").child(uid).update({
points: "2000"
})
要增加/減少用戶的點數,您可以使用原子增量運算子:
const uid = firebase.auth().currentUser.uid;
firebase.database().ref("user").child(uid).update({
points: firebase.database.ServerValue.increment(2000);
})
請注意,這僅在您將積分存盤為數字時才有效,因此:
firebase.database().ref("user").child(uid).update({
points: 2000 // ?? without quotes
})
然后
firebase.database().ref("user").child(uid).update({
points: firebase.database.ServerValue.increment(2000);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444485.html
