Firestore 请求中何时指定 writeFields 以及替换它们的内容?

cre*_*not 4 firebase firebase-security google-cloud-firestore

模拟器现在显示一条错误消息,试图访问request.writeFields.
在此之前,writeFieldsFirestore 安全规则在实际请求中不起作用。

该消息指出以下内容:

模拟器只模拟客户端SDK调用;对于这些模拟,request.writeFields 始终为 null

这是否意味着writeFields仅在 HTTP 请求中指定?

文档仅指出此

writeFields:写入请求中写入的字段列表。

由此产生的一个问题

我正在寻找替换此属性的内容,因为它是"always null"
request.resource.datainupdate还包含不在 requests中但据我所知已经在文档中的字段。

例子

// Existing document:

  document:
    - name: "Peter"
    - age: 52
    - profession: "Baker"

// Update call:

  document:
    - age: 53

// request.resource.data in allow update contains the following:

  document:
    - name: "Peter"
    - age: 53
    - profession: "Baker"
Run Code Online (Sandbox Code Playgroud)

但我只想要age.

Dou*_*son 11

编辑 2020 年 3 月 4 日:Map.diff()替换writeFields功能

Map.diff()函数给出了两个地图之间的差异:https : //firebase.google.com/docs/reference/rules/rules.Map#diff

在规则中使用它:

// Returns a MapDiff object
map1.diff(map2)
Run Code Online (Sandbox Code Playgroud)

一个MapDiff对象有以下方法

addedKeys() // a set of strings of keys that are in after but not before
removedKeys() // a set of strings of keys that are in before but not after
changedKeys() // a set of strings of keys that are in both maps but have different values 
affectedKeys() // a set of strings that's the union of addedKeys() + removedKeys() + updatedKeys()
unchangedKeys() // a set of strings of keys that are in both maps and have the same value in both
Run Code Online (Sandbox Code Playgroud)

例如:

// This rule only allows updates where "a" is the only field affected
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["a"])
Run Code Online (Sandbox Code Playgroud)

编辑 2018 年 10 月 4 日:writeFieldsFirestore 不再支持,其功能最终将被删除。

writeFields仍然有效,正如您从链接的文档中看到的那样。模拟器中的错误消息告诉您它无法模拟writeFields,因为它仅适用于来自客户端 SDK 的请求。模拟器本身似乎无法完全按照要求模拟请求,以便对 writeFields 进行测试。因此,如果您编写使用 的规则,则writeFields必须使用客户端 SDK 来测试它们以执行会触发规则的读取或写入。