简体   繁体   English

创建 Firestore 文档时获取权限错误

[英]Getting permission error when creating a Firestore document

I'm trying to securely create a Firestore document without requiring user authentication and have defined the following rule to do so:我试图在不需要用户身份验证的情况下安全地创建 Firestore 文档,并为此定义了以下规则:

      allow create: if request.resource.data.deviceid == resource.id;

According to what I've read from Google, this should allow the document to be created if the pending write includes a field called 'DeviceID' that matches the document ID, which is what I think I'm doing (I confirmed that androidID is identical to the document ID):根据我从谷歌上读到的内容,如果待处理的写入包含与文档 ID 匹配的名为“DeviceID”的字段,这应该允许创建文档,这就是我认为我正在做的(我确认 androidID 是与文档 ID 相同):

        data.put("DeviceID", androidID);
        mDevIDDocRef.set(data)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "DocumentSnapshot successfully written!");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                    public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error writing document", e);
                }
            });

However, the above fails with a permission exception.但是,上述操作因权限异常而失败。 What am I doing wrong?我究竟做错了什么?

During the create rule evaluation, the resource does not exist yet, so you have to read its future value: request.resource.data.whatever在创建规则评估期间,资源尚不存在,因此您必须读取其未来值:request.resource.data.whatever
To access an automatically generated document id, you need to read it from the document path:要访问自动生成的文档 ID,您需要从文档路径中读取它:

match /myCollection/{docId} {
    allow create: if request.resource.data.deviceid == docId;
    ...
}

暂无
暂无

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

相关问题 从 Firestore 获取文档时的错误处理 - Error handling when getting document from Firestore 尝试使用 Ionic4 和 Angular 6 将文档添加到 firebase 时出错 - 错误:PERMISSION_DENIED:权限被拒绝 - Getting error when trying to add document to firebase using Ionic4 and Angular 6 - Error: PERMISSION_DENIED: Permission denied 从 Firestore 文档中检索数字时出错 - Getting an error while retrieving a number from a Firestore document flutter firestore 未在云 firestore 数据库中创建文档 - flutter firestore not creating a document in the cloud firestore database 未创建文档的 Cloud Firestore 子集合 - Cloud Firestore Sub-Collection on a Document not Creating 从第 3 方 API 收集数据时如何更新 Firestore 文档而不是创建新文档 - How to update Firestore document when data changes when collected from 3rd Party API instead of creating new document 通过 SSH 连接服务器时出现错误 Permission denied (publickey) - Getting an error Permission denied (publickey) when connecting the server via SSH 由于每个文档每秒写入 1 次限制而导致写入失败时,Firestore 是否会出错或重试? - Does Firestore through error or retries when write failed due to the 1 write per sec per document limitation? 如果 firestore 文档不存在,如何抛出错误? - How to throw an error if firestore document does not exist? Firebase Cloud Firestore:在集合中为每个 UID 创建一个新文档 - Firebase Cloud Firestore: Creating a new document in a collection for every UID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM