Firebase规则.validate返回:权限被拒绝

时间:2019-08-21 07:55:38

标签: javascript firebase web firebase-realtime-database firebase-security

我正在尝试在数据库中设置一个值,以在规则中进行验证。我想确保设置的分数实际上是分数。

添加到数据库中的新分数应为:

初始得分 * 玩家等级 + 当前得分

这是我的数据库规则:

    "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())"
          }
        }
    }

我这样调用write函数:

    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); }
    });

当我管理日志data时,我得到:

{score: 2376, last_updated: 1566372843943}

这是我数据库中的数据:

{
  "initial_data" : {
    "score" : 200
  },
  "scores" : {
    "h3***********FAn2" : {
      "score" : 376,
      "last_updated" : 1566231627824
    }
  },
  "users" : {
    "h3***********FAn2" : {
      "level" : 10,
      "timestamp" : 1566226654955
    }
  }
}

,然后在控制台中收到错误消息:

  

错误:PERMISSION_DENIED:权限被拒绝

     

在仓库:632

     

在Et(util.ts:585)

     

at ci.callOnCompleteCallback(Repo.ts:624)

     

在仓库:356

     

在PersistentConnection.ts:523

     

......

1)我在哪里出错了,该如何解决?

2)我认为问题出在root.child()无法正确返回。有没有办法查看它在Firebase控制台中的评估结果?

1 个答案:

答案 0 :(得分:0)

已修复: 我不得不将我的写作能力提高到一个水平:

"scores": {
    "$user": {
      ".write": "auth.uid === $user",
      "score": {
        ".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())"
      }
    }
}

我不确定为什么,有人可以解释吗?