【问题标题】:Firestore error limit_to_first, can't read firestore valuesFirestore 错误 limit_to_first,无法读取 Firestore 值
【发布时间】:2020-11-14 12:38:00
【问题描述】:

我正在尝试从 firestore 获取数据,虽然它以前可以工作,但现在它已停止工作,并且我不断收到此错误 -

[Firestore]:侦听查询(target=Query(users/default/critical/home order by name);limitType=LIMIT_TO_FIRST) 失败:状态{code=PERMISSION_DENIED,description=Missing 或权限不足。,cause=null}

我的数据库规则是这样定义的-

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
                /*-----tile menus----------*/
        match /users/defaultmenuItems/{document=**} {
          allow read: if isOwner(userId); 
          allow write: if false;
        }

        match /users/{userId}/{document=**} {
          allow read,write,update: if isOwner(userId); 
        } 

        function isOwner(userId) {
          return request.auth.uid == userId;
        }
    } 
}

在 Android 中执行查询的代码

public void readMenu(final String collection, final String path, final DataRead dataStatus) {
        firebaseFirestore.collection(collection)
                .document(userId + path)
                .get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot documentSnapshot = task.getResult();
                            if (documentSnapshot.exists()) {
                                dataStatus.DataIsRead(documentSnapshot);
                            } else {
                                getDeafaultItems(collection, path);
                            }
                        } else {
                            Log.d(TAG, "Sorry ....");
                        }
                    }
                });
    }

private void getDeafaultItems(final String collection, final String path) {
        Log.d("MY_PATH",collection + "/defaultmenuItems"+path);
        firebaseFirestore.collection(collection)
                .document("defaultmenuItems" + path)
                .get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        if (documentSnapshot.exists()) {
                            copyFromDefaults(collection, documentSnapshot, path);
                        }
                    }
                });
    }

而Query是这样的-

users/defaultmenuItems/critical/home  

default 查询存在错误,我已使用上述正确路径进行了修复。但问题依然存在。

【问题讨论】:

  • 没有看到执行查询的代码,我们无能为力。该查询显然被某些规则拒绝,但无法判断是哪一个或如何解决它。请编辑问题并提供更多详细信息,并说明哪些地方没有按您预期的方式工作。
  • @DougStevenson 我已经添加了代码。你能查一下吗?
  • 仍然没有足够的信息。我们看不到您在查询中使用的变量的值,因此我们不知道您正在尝试使用哪个集合或文档。硬编码所有值,以便我们可以看到发生了什么。任何人都应该能够获取您的代码并重现该问题。
  • @BLΛCK,你确定request.auth.uid 的值是default 吗?尝试将isOwner 更改为始终返回true。它会起作用吗?
  • @DougStevenson 我已经更新了我的问题。我的查询中有一个错误,但问题仍然存在。

标签: android firebase google-cloud-firestore firebase-security


【解决方案1】:

这条规则:

        match /users/defaultmenuItems/{document=**} {
          allow read: if isOwner(userId); 
          allow write: if false;
        }

指的是一个变量userId,这里根本不存在。该名称没有通配符,因此userId 没有价值。目前尚不清楚您期望此值来自何处,但您需要重写此规则(或重构您的数据)以更准确地表达您的安全要求。

【讨论】:

  • 是的,我已经知道了。但是非常感谢您的帮助:) :) 安全 :)
猜你喜欢
  • 2020-03-27
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 2019-04-30
  • 2022-01-23
相关资源
最近更新 更多