【发布时间】: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