繁体   English   中英

如何避免 firestore 中的子集合拒绝权限?

[英]how to avoid permission denied with a subcollection in firestore?

当我试图阅读我的子集时,我的权限被拒绝了。

这是我的收藏和子收藏,我有 Teams/membersList

在此处输入图像描述

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  //Grants only a user access to its own data 
    match /users/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
    //Allow requests from authenticated users
    match/Users/{document=**}{
    allow read, write: if request.auth != null;
    }
    match/Teams/{document=**}{
    allow read, write: if request.auth != null;
    match /membersList/{membersList} {
          allow read, write: if request.auth != null;
        }
  }
   match /Teams/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
  match /Teams/memberLists/{document=**}{
  allow read, write: if request.auth != null;
  }
}

}


这是我的代码的一部分,但权限被拒绝

 let fetch = async () => {
    firestore()
      .collection("Teams")
      .where("uid", "==", await AsyncStorage.getItem("userID"))
      .get()
      .then((querySnapshot) => {
        if (querySnapshot.empty) {
          console.log("no documents found");
        } else {
          querySnapshot.forEach(async (doc) => {
            let Teams = doc._data.Activity;
            console.log(Teams);
            updateActivity((arr) => [...arr, Teams]);
            console.log(Activity);

            firestore()
              .collection("membersList")
              .get()
              .then((documentSnapshot) => {
                console.log("User exists: ", documentSnapshot.exists);

                if (documentSnapshot.exists) {
                  console.log("User data: ", documentSnapshot.data());
                }
              });
          });
        }
      });

编写此规则后,我的权限仍然被拒绝。 你有什么想法??

这段代码:

firestore()
  .collection("membersList")
  .get()

这从名为membersList的顶级集合中读取,该集合在您的规则中不存在。 所以用户无权访问数据,并且读取被拒绝。 即使集合不存在,读取也会被拒绝。

如果你想从当前团队的membersList子集合中读取,那就是:

doc
  .ref
  .collection("membersList")
  .get()

暂无
暂无

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

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