2017-07-25 141 views
0

我已阅读关于规则的Firebase文档以及如何验证写入数据库的新数据。有一条线我不明白某件事。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()用于什么?

回答

2

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

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

当你说root.child("users"),你进入它包含了用户的地图。

而当你说root.child("valid colors/blue")将去看看有效颜色的子图。

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

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

它存在于你的数据库中吗?不,不在所提供的示例中。这就是为什么它会失败。蓝色存在于您的数据库中。当你输入新数据时,它会通过规则。

+0

谢谢,它真的有帮助! – UmarZaii