firebase.database().ref("/user/" user.uid).set({
email:user.email,
points:"500",
uid: user.uid
})
我可以比較我的資料庫中的點和需要的點并執行任務嗎?就像如果用戶在資料庫中有 500 分,但需要 500 分才能在 Web 應用程式上執行操作,是否可以這樣做?任何建議,將不勝感激。
uj5u.com熱心網友回復:
如果要根據當前值對資料庫中的路徑執行操作,則需要使用事務:
firebase.database().ref("user").child(user.uid).child("points")
.transaction((currentValue) => {
if (parseInt(currentValue) > 500) {
... do you operation and currentValue and return the result to be written to the database
}
})
正如我在回答您之前的問題中所說的那樣,我建議將點的值存盤為數字,這樣您就不必在代碼中決議它,從而可以對其進行數字操作。
我建議此時花一些時間閱讀 Firebase 檔案。現在在那里度過幾個小時將避免許多這樣的問題。
除了(并且可能代替)此客戶端代碼之外,您還需要驗證資料庫安全規則中的點值。例如,如果點必須始終為正數:
{
"rules": {
...
"user": {
"$uid": {
"points": {
".validate": "newData.isNumber() &&
newData.val() >= 0"
}
},
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444484.html
標籤:javascript 火力基地 firebase-实时数据库
