繁体   English   中英

如何在 Cloud Firestore 中获取子集合的名称

[英]how to get the name of subcollection in cloud firestore

我有数据库(如下图),我需要获取包含测验名称的子集合的名称

在此处输入图像描述

没有办法获得集合名称(肯定在客户端)。 集合就像常规数据库中的表名。 如果你不知道你可以在集合中拥有什么,而你让用户创建他们自己的集合,那么你的数据库最终会被一些黑客使用。 这就是规则在 Firebase 中的样子。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // This line prevent anyone except server to manipulate database.
    // All other lines overwrite this rule and let do something in database for users.
    match /{document=**} { 
      allow read, write: if false;
    }
    // Here, users can create and update their documents, but they cannot delete them.
    // Because of rules above, they cannot create nested collection.
    // This is important because someone may do his own nested collection and use it to his project.
    match /bookings/{docId} {
      allow read: if resource.data.owner == request.auth.uid || isAdmin()
      allow update: if resource.data.owner == request.auth.uid || isAdmin()
      allow create: if request.auth != null
    }
    match /app/{docId} {
      allow read: if true;
      allow write: if false;
    }
  }
}

function isAdmin() {
    return request.auth.token.admin == true;
}

测验规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} { 
      allow read, write: if false;
    }
    match /quizzes/{docId} {
      allow read: if resource.data.public == true || resource.data.owner == resuest.auth.uid
      allow update, detele: if resource.data.owner == request.auth.uid
      allow create: if request.resource.data.owner == request.auth.uid
    }
  }
}

TypeScript 中的示例查询网页:

function getQuizzes() {
   const ref = collection('quizzes')
   const qRef = query(ref, ...[where("level", ">=" 1), where("public", "==", true ), where("category", "==", "math"), order("level", "asc")])
   return getDocs(qRef)
}

代码可能有问题。 我没有测试它。

暂无
暂无

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

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