仅当子项存在时,Firebase才会使用规则setValue

问题描述 投票:2回答:2

我正在尝试使用规则,但它不起作用。

这是结构:

enter image description here

我有这个命令:DeleteItemFromListRef.child("List").child(obj.getsEventID()).child(ID).child("check").setValue(false);

但是我想只在子(键)存在的情况下将值设置为“false”,这样如果项目被删除,我不会只是这样:

enter image description here

这是我的规则:

 "List": {
    "$listID" : {
      "$key": {
        "check" : {
      ".write":"data.child('List').child($listID).child($key).child('key').exists()"
    }
  }
}

}

但是,如果子键为空,它仍然会上传这样的数据:

enter image description here

如果有人可以提供帮助,那就太好了

编辑2:这里有更简单的规则

"rules": {






           "List": {
            "$listID" : {
               "$key": {
                 "check" : {  
".read":"auth.uid != null", 
".write":"auth.uid != null", 
".validate":"data.parent().child('key').exists()"

}
}}}}}

+评论:

所以我总是用地图更新项目如果我添加一个新的,但是如果我只更改子项“check”我使用“setValue”方法并且只有当项目已经存在时才想要完成setValue (== child key already exist)

android firebase firebase-realtime-database firebase-security
2个回答
1
投票

如果你只想允许写checked,如果它的兄弟属性key存在,你可以这样做:

"List": {
    "$listID" : {
        "$key": {
            "check" : {
                ".validate":"data.parent().child('key').exists()"
            }
        }
    }
}

更新由于您无法完成这项工作,我只是在一个非常简单的设置中对其进行了测试。在我的数据库中的/55431756下,我有这个JSON:

{
  "has_key" : {
    "key" : "value"
  }
}

所以它是一个单一的关键has_key有一个key。现在我运行了两个模拟:一个尝试写入has_key

写作has_key成功,因为has_key/key存在:

Writing to <code>has_key</code> is allowed

写入no_key被拒绝,因为没有no_key/key孩子:

Writing to <code>no_key</code> is denied


2
投票

那么,你也可以先检查孩子是否存在,然后在true时执行上述命令。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.