如何在安全规则中使用变量构建Firestore文档的路径

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

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

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

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

value-1:value-2

其中“值-1”来自文档字段“ field1”,“值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-rules
1个回答
0
投票

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

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

exists(
  /databases
  /$(database)
  /documents
  /Side_Collection
  /$(request.resource.data.field1 + ":" + request.resource.data.field2);
© www.soinside.com 2019 - 2024. All rights reserved.