Firestore:尝试添加新的子集合时缺少权限或权限不足

问题描述 投票:1回答:1

目前,我具有一些访问Firestore数据库的基本规则。规则如下。谁能帮助我了解为什么第二段代码会导致权限错误?我正在尝试将图像添加到尚不存在的子集合中。谢谢

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        match /projects/{project} {
            allow read, write: if request.auth.uid != null
        }
        match /users/{userId} {
            allow create
            allow read: if request.auth.uid != null
            allow write: if request.auth.uid == userId
        }
    match /notifications/{notification} {

            allow read: if request.auth.uid != null

        }

    }
}

这是我的应用代码:

await firestore.add(
      {
        collection: "users",
        doc: user.uid,
        subcollections: [{ collection: "photos" }]
      },
      {
        name: imageName,
        url: downloadURL
      }
    );
javascript firebase google-cloud-firestore firebase-security-rules
1个回答
1
投票

我对“ reduxfirestore”一无所知,但似乎您正在尝试写入用户下的子集合。您的安全规则对子集合中的文档无话可说,因此所有这些查询都将被拒绝。如果您希望能够读写文档,则规则必须匹配其完整路径,如下所示:

match /users/{uid}/photos/{pid} { ... }

在控制台中进行读取和写入始终有效,因为它绕过了安全规则。

© www.soinside.com 2019 - 2024. All rights reserved.