I am trying to set a value in the database which validates in the rules. I want to make sure that the score been set is actually the score.
The new score added to the database should be:
initial score *
the players level +
their current score.
Here is my database rule:
"scores": {
"$user": {
"score": {
".write": "auth.uid === $user",
".read": "auth.uid === $user",
".validate": "newData.isNumber() && newData.val() === ((root.child('initial_data').child('score').val() * root.child('users/' + auth.uid).child('level').val()) + root.child('scores/' + auth.uid).child('score').val())"
}
}
}
and I am calling the write function like so:
var data = {
balance: score + (initial_score * level),
last_updated: last_updated
}
var ref = fb.ref().child('scores/' + userId).set(data, function (err) {
if (err) { console.error(err); }
});
When I console log data
I get:
{score: 2376, last_updated: 1566372843943}
Here is the data in my database:
{
"initial_data" : {
"score" : 200
},
"scores" : {
"h3***********FAn2" : {
"score" : 376,
"last_updated" : 1566231627824
}
},
"users" : {
"h3***********FAn2" : {
"level" : 10,
"timestamp" : 1566226654955
}
}
}
and in the console I get the error:
Error: PERMISSION_DENIED: Permission denied
at Repo.ts:632
at Et (util.ts:585)
at ci.callOnCompleteCallback (Repo.ts:624)
at Repo.ts:356
at PersistentConnection.ts:523
......
1) Where am I going wrong and how do I fix it?
2) I think the issue is with the root.child() not returning correctly. Is there a way to see what this evaluates as in firebase console?
Aucun commentaire:
Enregistrer un commentaire