Firestore 规则:检查数组中是否存在值

Firestore rules : check the existence of a value in array

我想只给members组里的用户读写权限,不知道为什么不行?

这是 firestore 中的规则:

    rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  match /Users/{uid} {
  allow write: if request.auth != null && request.auth.uid == uid;
  allow read : if request.auth != null;
  }
    
    match /GroupChat/{document=**} {
      allow read,create,update: if request.auth != null && request.auth.uid in request.resource.data.Members;
        
    }
  }
}

这是我数据库的截图

查询:

FirebaseFirestore.instance
          .collection("GroupChat")
          .where("Members",
              arrayContains: FirebaseAuth.instance.currentUser!.uid)
          .snapshots(),

当我想在我的应用程序中显示信息时,出现此错误:

W/Firestore(14600): (24.0.0) [Firestore]: Listen for Query(target=Query(GroupChat where Members array_contains 0FQhApDgWMVNTpOiDewF3qJX7IA3 order by name);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

documentation

中所述

The resource variable refers to the requested document, and resource.data is a map of all of the fields and values stored in the document.

但是,request.resource.data 包含在 update/write 操作中添加到文档中的数据。您应该使用 resource.data,因为您要检查现有数据。

match /GroupChat/{document=**} {
  allow read,create,update: if request.auth != null && request.auth.uid in resource.data.Members;        
}