仅当UID与Firebase Firestore中的集合名称匹配时,才如何读取集合? [英] How to read collection only if UID matches collection name in Firebase Firestore?

查看:36
本文介绍了仅当UID与Firebase Firestore中的集合名称匹配时,才如何读取集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为每个用户创建了一个用户ID集合,并且我希望设置数据库规则以仅在用户ID与收集项目名称匹配时才读取该收集项目.我尝试过类似的事情:

I created a Collection of user IDs for each user and I want to set the database rules to read the collection item only if the user ID matches the collection item name. I tried something like:

 service cloud.firestore {
    match /databases/{database}/documents {
      match /{document=**} {
        allow read: if request.auth != null;
      }
      match /{userId} {
        allow read: if belongsTo(userId);
      }
      function belongsTo(userId) {
        return request.auth.uid == userId
      }
    }
    }

推荐答案

Firestore读取文档.没有阅读馆藏的概念.因此,您的规则必须提供对文档的访问权限.

Firestore reads documents. There is no concept of reading a collection. So your rules must provide access to documents.

要授予对以用户的UID命名的集合中所有文档的访问权限,请执行以下操作:

To grant access to all documents in the collection named after the user's UID:

  match /{userId}/{document} {
    allow read: if belongsTo(userId);
  }

这篇关于仅当UID与Firebase Firestore中的集合名称匹配时,才如何读取集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆