【问题标题】:How to build a path to a Firestore document in security rules using variables如何使用变量在安全规则中构建 Firestore 文档的路径
【发布时间】:2019-11-21 16:13:55
【问题描述】:

我正在编写一个规则以允许向 My_Collection 创建新文档,其结构如下所示:

field1="value-1"
field2="value-2"
field3="other-miscellaneous-values"

只有在 Side_Collection 中没有文档 ID 使用以下格式的文档时才允许这样做:

value-1:value-2

其中“value-1”来自文档字段“field1”,“value-2”来自“field2”,并且用冒号分隔。

这是我正在尝试的规则:

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


【解决方案1】:

如果您想在路径构造中插入一些变量值或表达式,您必须使用$() 来隔离该表达式。这在accessing other documents 的文档中进行了讨论。

在您的情况下,您可能希望像这样构建传递给 exists() 的路径(为了便于阅读,我添加了回车符,您需要删除它们):

exists(
  /databases
  /$(database)
  /documents
  /Side_Collection
  /$(request.resource.data.field1 + ":" + request.resource.data.field2);

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2019-08-26
    • 2023-04-09
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多