gpt4 book ai didi

firebase - 何时在 Firestore 请求中指定 writeFields 以及什么替换它们?

转载 作者:行者123 更新时间:2023-12-02 19:18:23 25 4
gpt4 key购买 nike

模拟器now displays an error message trying to access request.writeFields .
在此之前,Firestore 安全规则中的 writeFields 在实际请求中不起作用。

消息内容如下:

The simulator only simulates client SDK calls; request.writeFields is always null for these simulations

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

documentation only states this :

writeFields: List of fields being written in a write request.

由此产生的问题

我正在寻找替代此属性的内容,因为它“始终为空”
update 中的 request.resource.data 还包含不在请求中但已在文档中的字段据我所知。

示例

// 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"

但我只想要年龄

最佳答案

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

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

要在规则中使用它:

// Returns a MapDiff object
map1.diff(map2)

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

例如:

// This rule only allows updates where "a" is the only field affected
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["a"])
<小时/>

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

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

关于firebase - 何时在 Firestore 请求中指定 writeFields 以及什么替换它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com