javascript - 允许在 Firestore 中更新特定的嵌套值
问题描述
我需要让匿名用户能够更新嵌套架构中的特定值,而无需授予文档其余部分的权限。
events
是顶级集合,我像这样更新我的文档
update(`events/${event.id}`, {
[`boardgames.${boardgameId}.votes.${uid}`]: true,
})
我已经尝试过这个解决方案
allow update: if ((request.writeFields.size() == 1) && ('boardgames' in request.writeFields));
正如这里提到的,但它失败了(两半也独立失败)。
我错过了什么?“进入”嵌套对象时,我不是只更新一个字段吗?
能够 console.logrequest
和request.writeFields
.
请注意,理想情况下,我只想允许修改最深的属性,即当前用户uid
在votes
.
解决方案
实际上,request.writeFields
捕获修改字段的完整路径,因此在您的情况下,它将等于boardgames.${boardgameId}.votes.${uid}
.
您可以使用带有String.matches的正则表达式
allow update: if request.auth.uid != null
&& request.writeFields.size() == 1
&& request.writeFields[0].matches("boardgames\.[^.]{5,25}\.votes\." + request.auth.uid)
小心: request.writeFields 已被弃用,但不幸的是,您的问题没有很好的替代品
推荐阅读
- swift - 无法转换“SharedSequence”类型的值
' 到预期的参数类型 - apache-kafka - 如何在不使用 Docker 或 Windows Server 2016 上的 Confluent 平台的情况下在 Kafka 中设置 Debezium SQL Server 连接器?
- javascript - 强制 Facebook 使用风格
- android - 如何在片段布局中创建微调器?
- python - 使用 pdf2image 从目录中提取 pdf 并将图像输出到不同的目录
- regex - htaccess 中重写规则 apache 中 ^ 与 ^.*$ 之间的区别
- reactjs - 如何使用 Flamelink 中的填充属性检索图像的属性?
- laravel - 如何在 Flutter 中使用 rest api 不使用 firebase 以 google 或 facebook 登录
- mongodb - Atlas搜索对象数组
- typescript - 打字稿:如果另一个属性与条件匹配,则声明一个接口属性被定义