Cloud Firestore规则,“允许创建”

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

这里是Cloud Firestore安全规则的问题。

可能很简单,但是由于我不掌握规则语法,因此无法正常工作。

我希望规则允许创建新文档:

newDoc(field1="value-1", field2="value-2", field3="other-miscellaneous-values")

在My_Collection中,只有在Side_Collection中没有文档ID为ID的文档时,

value-1:value-2

这是我尝试的规则:

  allow create: if !exists(/{database}/Side_Collection/{request.resource.data.field1}:{request.resource.data.field2});

这是我收到的错误消息:

Error saving rules - Line 24: Unexpected '}'.; Line 24: Missing 'match' keyword before path.; Line 24: Unexpected '.'.; Line 24: mismatched input 'request' expecting '}'; Line 24: Unexpected ':'.; Line 24: Unexpected ')'.; Line 30: Unexpected 'match'. 

我尝试了上述几种变体,但无济于事。

任何指导将不胜感激。

firebase google-cloud-firestore firebase-security-rules
1个回答
0
投票

您尝试使用安全规则无法执行的操作。规则无法对集合进行任意查询。如果您知道文档的完整路径,则只能get()。知道其中一个字段是行不通的,因为这需要查询。

您可以做的是让您的客户端通过Cloud Functions来执行查询和检查条件。因此,您可以实现客户端可以调用的HTTP(或可调用)类型的函数。或者,您可以实现Firestore onCreate触发器以在添加文档后检查该文档,如果文档无效,则将其删除。

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