Firebase:如何读取父级中的每个子节点(具有特定数据的子节点除外) [英] Firebase : How to read every child node in parent except children with particular data

查看:89
本文介绍了Firebase:如何读取父级中的每个子节点(具有特定数据的子节点除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下内容.

posts : {
      "$postid":{
               "meta":{
                      "state":true,
                      "text" :"I should be read",
               },
               "likes":{
                      "uid1":true,
                      "uid2":true,
               },
       },
       "$postid":{
               "meta":{
                      "state":false,
                      "text" :"I should be read",
               },
               "likes":{
                      "uid1":true,
                      "uid2":true,
                      "uid3":true,
               },
       }
}

我要查询父节点("posts"),如下所示.

I want to query the parent node('posts') as follows.

  • 省去状态为false的帖子
  • 最喜欢的帖子

安全规则

  • 拒绝阅读状态为== false的帖子.

这是我查询数据的Javascript版本. ////$.fdr === firebase.database.ref();

Here's the Javascript Version Of how I'm querying data. ////$.fdr === firebase.database.ref();

$.fdr("posts").orderByChild("time").limitToLast(20).once('value',function(r) {
    var r = r.val();
    if(r !== null){                 
        ///use r;  
    }
},function(e){
        console.log(e);
}); 

原因:我希望用户删除帖子,以后可以撤消删除. 要删除,我将"post.meta.state"的值更改为"false". 尽管"post.meta.state"为假,但安全规则不应允许读取该帖子.

Why this: I want the user to delete a post, and be able to revoke the delete later on. To delete I change the value of "post.meta.state" to "false". While "post.meta.state" is false, security rules shouldn't allow that post to be read.

问题:如果我在发布节点而非父节点("posts")中设置规则,则

Problem : If i set rules inside the post node rather than the parent node("posts"),

 "postid": { 
             ".read":"data.child('state').val() == true"
           }

然后我可以限制".read",但前提是我要按以下方式查询帖子:

then I can restrict ".read", but only if i'll query posts as follows :

firebase.database.ref("posts/postid");

否则,如果我尝试从父节点进行查询,例如:

Otherwise if I try to query from the parent node like:

firebase.database.ref("posts/"), 

以便我可以过滤掉我不希望返回的帖子,然后必须将规则从"posts"子级提升为"posts"(父级),这将导致忽略子级中的规则,而我似乎找不到拒绝"state" == false;的帖子的方法.

so that I can filter out posts I don't want returned, then I'll have to elevate the rules from "posts" children to "posts"(parent), which will cause the rules in the children to be ignored, and there I can't seem to find a way to deny returning posts with "state" == false;

以上内容是基于我到目前为止的理解,请在我迷路的地方更正.

推荐答案

不幸的是,当用户具有读取/写入父节点的权限时,无法在任何子节点上阻止读取/写入.您可以在 Firebase文档中了解有关安全规则如何级联的更多信息.

Unfortunately, it is not possible to block reading/writing on any child node when a user as permission to read/write the parent node. You can read more about how security rules cascade in the Firebase Docs.

也就是说,有两种可能的解决方案供您考虑:

That said, there are a couple possible solutions for you to consider:

  1. 您可以将其移至单独的父节点,而不是将其标记为已删除.然后,您可以调整Deleted_posts的安全规则以将其隐藏.

  1. Instead of marking a post as deleted, you can move it out to a separate parent node. Then you can adjust the security rules for deleted_posts to hide it.

{
  "posts": {
    "$postid1": {
      "meta": {
        "state": true,
        "text": "I should be readable"
      },
      "likes": {
        "uid1": true,
        "uid2": true,
        "uid3": true
      }
    }
  }
  "deleted_posts": {
    "$postid2": {
      "meta": {
        "state": false,
        "text": "I should not be readable"
      },
      "likes": {
      }
    }
  }
}

  • 保留可见的postid的单独列表,并将"posts"节点设置为不可读,但将子级设置为可读(如果未隐藏).您将必须进行第二次查询才能获取每个帖子的数据:

  • Keep a separate list of postids that are visible, and set the "posts" node to not readable, but the children to readable if not hidden. You will have to do a second query to get the data for each post:

    {
      "posts": {
        "$postid1": {
          "meta": {
            "state": true,
            "text": "I should be readable"
          },
          "likes": {
            "uid1": true,
            "uid2": true,
            "uid3": true
          }
        },
        "$postid2": {
          "meta": {
            "state": false,
            "text": "I should not be readable"
          },
          "likes": {}
        },
        "$postid3": {
          "meta": {
            "state": true,
            "text": "I should be readable"
          },
          "likes": {}
        }
      },
      "visible_posts": {
        "$postid1": true,
        "$postid3": true
      }
    }
    

  • 这篇关于Firebase:如何读取父级中的每个子节点(具有特定数据的子节点除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆