Cloud Firestore:我的 Flutter 应用的规则有问题

Cloud Firestore: Problem with rules for my Flutter app

这是我当前的 Cloud Firestore 规则:https://pastebin.com/rQBacfLm

在此屏幕截图中,您可以看到我当前的数据库结构:

这是我的 Flutter 应用程序中的代码,我试图用它来获取“所有者”等于用户 UID 的船只流:

_databaseReference.collection("boats").where("owner", isEqualTo: "My UID").snapshots()

不知道是规则有普遍性的错误,还是跟直播有关系。我认为这可能是规则的问题,但我不知道哪里出了问题。有人可以发现规则中的错误吗?

这是来自控制台的日志:

W/Firestore(26299): (22.0.1) [Firestore]: Listen for Query(target=Query(boats where owner == # com.google.firestore.v1.Value@628107b8
W/Firestore(26299): integer_value: 0
W/Firestore(26299): string_value: "My UID" order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
W/Firestore(26299): (22.0.1) [Firestore]: Listen for Query(target=Query(boats where share array_contains # com.google.firestore.v1.Value@628107b8
W/Firestore(26299): integer_value: 0
W/Firestore(26299): string_value: "My UID" order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

重要信息:“我的UID”只是一个占位符,实际上是我的真实UID。那不是问题!

如果

,您允许阅读船文件
    function isOwner(boatId, db) {
      return get(/databases/$(db)/documents/boats/$(boatId)).data.owner == request.auth.uid;
    }

所有者字段等于用户身份验证 ID。但是在您文档的屏幕截图中,我认为它的值 My UID 不是您正在查询的有效 ID。检查这里是否有问题

如您在我的 Firestore 规则 (https://pastebin.com/rQBacfLm) 中所见,这些是从 boats 集合访问文档的条件:

match /boats/{boat} {
  allow create: if isUser(database);
  allow read, update: if isUser(database) && isShared(boat, database);
  allow read, update, delete: if isUser(database) && isOwner(boat, database);
}

当我现在执行_databaseReference.collection("boats").where("owner", isEqualTo: "My UID")时,我得到一个PERMISSION_DENIED错误,因为where需要权限list,它包含在权限[=16=中],但 read 权限仅适用于 isSharedtrue 的船只。因此需要在allow create行添加list权限,以便应用程序可以搜索用户拥有的船只(.where("owner", isEqualTo: "My UID"))。