简体   繁体   中英

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. 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:

  1. There is no data at the path /users/Blog .
  2. Nobody had permission to read /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.

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:

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"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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