Firebase Firestore:有没有办法强制集合中所有文档的必填字段? [英] Firebase Firestore: Is there a way to enforce required fields for all documents in a collection?

查看:23
本文介绍了Firebase Firestore:有没有办法强制集合中所有文档的必填字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在创建的应用程序手动添加数据.我必须将许多文档添加到一个集合中,所有这些文档都必须包含相同的 5 个字段.当我在 Firebase 控制台中创建新文档时,有没有办法自动创建这些字段?并且还要强制所有文档都包含这 5 个字段(这样我就不会出错)?

I am manually adding data for an app I am creating. I have to add many documents to a collection, all of which have to contain the same 5 fields. Is there a way to automatically create these fields when I create a new document in the Firebase console? And to also enforce all documents have those 5 fields (so that I don't make mistakes) ?

此外,有没有比从 Firebase 控制台手动添加这些数据更好的方法?例如通过 JSON?

Additionally, is there a better way to manually adding this data than from the Firebase console? Through JSON for example?

推荐答案

这里有两个单独的问题.这两个问题之前都已经回答过,但从来没有在一个问题中回答过,所以我会在这里回答.

There are two separate questions here. Both have been answered before, but never in a single question, so I'll do that here.

这是不可能的.您将明确地将这些值写入您创建的每个新文档中.

This is not possible. You will have explicitly write those values into each new document you create.

这可以通过使用 Firebase 的服务器端安全规则来实现.

This is possible through the use of Firebase's server-side security rules.

例如,要确保文档具有 field1field2field3 的值,您可以使用 hasAll:

For example, to ensure a document has values for field1, field2 and field3, you could use hasAll:

allow write: if resource.data.keys().hasAll(['editor', 'admin']);

这篇关于Firebase Firestore:有没有办法强制集合中所有文档的必填字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆