Firebase 如何在firestore中编写以下规则?

Firebase 如何在firestore中编写以下规则?,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,我正在试图弄清楚如何编写firestore规则,以便只有文档键值对的键中匹配的uid才能拥有写入权限 更具体地说: 我的firestore结构类似于: 'Items LA' -> 'Pasadena' -> {'awjij53dHh3dnYAh': {itemInformation}} collection -> document -> fields 其中,“awjij53dHh3dnYAh”是卖方ID 我想写一个规则,这样它就可以给awjij53dHh3dnYAh在该

我正在试图弄清楚如何编写firestore规则,以便
只有文档键值对的键中匹配的uid才能拥有写入权限

更具体地说:

我的firestore结构类似于:

'Items LA' -> 'Pasadena' -> {'awjij53dHh3dnYAh': {itemInformation}}
collection -> document -> fields
其中,
“awjij53dHh3dnYAh”
是卖方ID

我想写一个规则,这样它就可以给
awjij53dHh3dnYAh
在该字段中写入的权限。

目前我所拥有的是:

  match /Items%20LA/{cityName}/{data}{
    allow read: if request.auth != null;
    allow write: if request.auth.uid == data.key; // This is where I am not sure how to write what I want to accomplish
  }

谢谢

你想要的可能是

allow write: if request.auth.uid == $(data)
//or this
allow write: if request.auth.uid == resource.id

首先-规则的匹配路径只包括文档的路径组件(集合和文档ID),而不包括字段。而且,它不需要任何URL转义。只需匹配“/Items LA/{cityName}”

其次,若要在规则表达式中引用文档中字段的名称,您应该知道这是所有传入字段的映射,并且必须使用其API来处理这些键/值对

第三,如果要找出文档的哪些字段发生了更改,则需要使用API比较更改前后文档的状态

将所有这些放在一起,您需要检查是否只有更改的字段等于用户的UID:

match /Items LA/{cityName} {
    allow write: if request.resource.data.diff(resource.data).affectedKeys().hasOnly([request.auth.uid]);
}

什么是
data.key
应该在这里?当我拿走转义%20的URL时,它给了我一个错误:错误保存规则-第5行:路径前缺少'match'关键字。;第5行:意外的“LA”。;第5行:不匹配的输入'LA'应为{'{','/',路径_段};第5行:路径前缺少'match'关键字;第6行:在'allow'处缺少'}';第62行:出乎意料的“}”。但除此之外,它就像一个符咒!谢谢你,道格!(我无法编辑您的答案,但在请求之前缺少if。资源)