【问题标题】:Firebase rules validation dataFirebase 规则验证数据
【发布时间】:2017-07-25 08:51:48
【问题描述】:

我已阅读有关规则以及如何验证写入数据库中的新数据的 Firebase 文档。有一行我不明白某件事的意思。

下面是Firebase Documentation的firebase规则代码。

{
  "rules": {
    ".write": true,
    "widget": {rules)
      ".validate": "newData.hasChildren(['color', 'size'])",
      "size": {
        ".validate": "newData.isNumber() &&
                      newData.val() >= 0 &&
                      newData.val() <= 99"
      },
      "color": {
        // the value of "color" must exist as a key in our mythical
        // /valid_colors/ index
        ".validate": "root.child('valid_colors/' + newData.val()).exists()"
      }
    }
  }
}

".validate": "root.child('valid_colors/' + newData.val()).exists()" 是我不明白它是什么以及它做什么的地方。

  • root.child 是什么?是否用于访问该颜色的子项?
  • 什么是newData.val()).exists()exists() 是干什么用的?

【问题讨论】:

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


    【解决方案1】:

    当您使用“root”时,它会一直回到顶部。想象一下这样的数据库:

    Database:
     -users
      -uid's
       -username
     -valid colors
      -blue
      -red
    

    当您说root.child("users") 时,您将进入包含用户的地图。

    当您说root.child("valid colors/blue") 时,将查看有效颜色的子图。

    Exists 检查它是否存在于数据库中。 newData 代表您要输入的数据。如果你想输入“绿色”,它会失败。这是因为如果你的 JSON 看起来像这样:

    "color" : green
    newData.val() = green
    

    它是否存在于您的数据库中?不,不在提供的示例中。这就是它会失败的原因。蓝色存在于您的数据库中。当您将其作为新数据输入时,它将通过规则。

    【讨论】:

    • 谢谢,真的很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-05-10
    • 1970-01-01
    • 2018-08-25
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多