简体   繁体   English

如何从 firebase 中的所有用户中检索特定的子数据

[英]How to retrieve a specific child data from all the users in firebase

I am creating a blogging app where the user can post after signing in with their account.我正在创建一个博客应用程序,用户可以在使用他们的帐户登录后发布。 My problem is that I want to be able to show all the blogs from all the users.我的问题是我希望能够显示所有用户的所有博客。 there is a child called Blog under all users with their Uid, which I want to display on my dashboard, so when a user login with their account they are able to see blogs from other users.在所有用户的 Uid 下都有一个名为 Blog 的子项,我想将其显示在我的仪表板上,因此当用户使用他们的帐户登录时,他们能够看到其他用户的博客。 I hope the question is clear.Can anyone please help me.我希望问题很清楚。任何人都可以帮助我。 Thank you.谢谢你。

This is my code, this however gives me error permission denied,这是我的代码,但这给了我错误权限被拒绝,

 private void loadPost() {
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users").child("Blog");
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    postList.clear();
                    for (DataSnapshot ds: dataSnapshot.getChildren()){
                        ModelPost modelPost = ds.getValue(ModelPost.class);

                        postList.add(modelPost);

                        adapterPost = new AdapterPost(getActivity(), postList);
                        recyclerView.setAdapter(adapterPost);
                    }


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(getActivity(), ""+databaseError.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });
        }

This is my database.这是我的数据库。

在此处输入图像描述

This is my database security rules这是我的数据库安全规则

"rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }

Your code tries to read from this getReference("users").child("Blog") , which has two problems:您的代码尝试从此getReference("users").child("Blog")读取,这有两个问题:

  1. There is no data at the path /users/Blog .路径/users/Blog中没有数据。
  2. Nobody had permission to read /users/Blog .没有人有权阅读/users/Blog

Your rules allow a user to read their own node, both blog posts and profile info.您的规则允许用户阅读他们自己的节点,包括博客文章和个人资料信息。 To read a user's own blog posts, you'd listen to:要阅读用户自己的博客文章,您会听到:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users").child(uid).child("Blog")

Note that this doesn't allow the user to read all blog posts from all users.请注意,这不允许用户阅读所有用户的所有博客文章。 In fact, in your current model that is not possible without also granting them access to read all user profiles, which I think is not what you want.事实上,在您当前的 model 中,如果不授予他们读取所有用户配置文件的权限,这是不可能的,我认为这不是您想要的。

If you want to allow users to read all blog posts, but only allow them access to their own profile, you will need to create two separate top-level nodes:如果您想允许用户阅读所有博客文章,但只允许他们访问自己的个人资料,则需要创建两个单独的顶级节点:

profiles: {
  uid1: {
  }
  uid2: {
  }
}
blogs: {
  uid1: {
    blogid1: { ... }
    blogid2: { ... }
  }
  uid2: {
    blogid3: { ... }
    blogid4: { ... }
  }
}

Alternatively you could model the blogs in that last structure as:或者,您可以 model 最后一个结构中的博客为:

blogs: {
  blogid1: { uid: "uid1". ... }
  blogid2: { uid: "uid1". ... }
  blogid3: { uid: "uid2". ... }
  blogid4: { uid: "uid2". ... }
}

In either case, you can now allow the access with:无论哪种情况,您现在都可以通过以下方式允许访问:

"rules": {
  "profiles": {
    "$uid": {
      ".read": "$uid === auth.uid",
      ".write": "$uid === auth.uid"
    }
  },
  "blogs": {
    ".read": true"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM