【问题标题】:Firebase rules .validate returns: Permission DeniedFirebase 规则 .validate 返回:权限被拒绝
【发布时间】:2019-12-26 10:55:51
【问题描述】:

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

添加到数据库的新分数应该是:

初始分数 * 玩家等级 + 他们当前的分数

这是我的数据库规则:

    "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:权限被拒绝

在 Repo.ts:632

在 Et (util.ts:585)

在 ci.callOnCompleteCallback (Repo.ts:624)

在 Repo.ts:356

在 PersistentConnection.ts:523

......

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    我不知道为什么,谁能解释一下?

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多