Javascript 没有使用Firebase安全规则显示数据

Javascript 没有使用Firebase安全规则显示数据,javascript,angular,google-cloud-firestore,firebase-security,Javascript,Angular,Google Cloud Firestore,Firebase Security,我正在努力保护我的火力基地 我这样做是为了保护我的用户列表 rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /about/{id} { allow read: if true; allow write: if request.auth.uid != null; } match /avantages/{id} {

我正在努力保护我的火力基地

我这样做是为了保护我的用户列表

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
     match /about/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /avantages/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /blog/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /customers/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /lowersection/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /middlesection/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
match /topsection/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
    
match /users/{userId} {
allow read: if isOwner(userId);
allow write: if isOwner(userId);
}
}
function isOwner(userId) {
return request.auth.uid == userId;
 }
}
但一旦我这样做了,我的网站上就不会显示任何数据

目标是让每个人都可以读取数据(用户集合除外),但数据只能由登录的用户写入

根据要求,下面是Angular应用程序的代码,允许它获取数据并发回更新的数据

getTopSectionMain() {
return   this.firestore.collection('topsection').doc('content').collection('main').snaps hotChanges();
}

updateTopSectionMain(dataID: any, data: any) {
this.firestore.doc('topsection/content/main/' + dataID).update(data);
}
这是我的内容服务

任何帮助都将不胜感激


干杯

您的问题似乎来自于您阅读子集合中的文档

让我们以
topsection
集合为例。您有如下安全规则:

match /topsection/{id} {
    allow read: if true;
    allow write: if request.auth.uid != null;
}
getTopSectionMain() {
    return   this.firestore.collection('topsection').doc('content').collection('main').snaps hotChanges();
}
但您可以按如下方式查询文档:

match /topsection/{id} {
    allow read: if true;
    allow write: if request.auth.uid != null;
}
getTopSectionMain() {
    return   this.firestore.collection('topsection').doc('content').collection('main').snaps hotChanges();
}
这意味着您可以查询
topsection
集合中
content
文档的
main
子集合的文档

如中所述:“安全规则仅适用于匹配的路径,因此(父)集合上定义的访问控制不适用于(子)子集合。”

您需要通过添加

或指定每个子集合,如:

match /topsection/{id} {
    match /main/{id} {
        allow read: if true;
        allow write: if request.auth.uid != null;
    }
}

由于在声明顶级集合的安全规则时没有使用通配符,因此最好使用第二个选项。

Ah yes,我似乎忘记了这一点,但这只会影响登录用户,而不会影响网站访问者?实际上,在您回答之前,我删除了我的评论:-)我刚才提到,对于
用户
集合,
读取
没有任何规则。因此,没有人可以读取此集合中的文档。请注意,通过刚才所做的修改(
match/users/{userId}{allow read:if true;
),任何人都可以读取
users
集合中的所有文档。我添加了“allow read:if true;”在用户部分。这应该可以实现您所提到的。但这仍然不起作用。而且我在我微薄的知识中找不到任何错误。请注意我上面的评论:通过此更改,您的目标(“让所有人都能阅读数据(用户集合除外)”)无法访问,因为任何人都可以阅读
用户
集合中的所有文档。感谢您的帮助。我使用了通配符(因为我懒得为每个子集合xD编写条目)。我知道它是如何工作的:)