我无法使用 Firestore 安全规则限制创建的文档中的字段数量。 https://firebase.google.com/docs/firestore/security/rules-structure
我的用户文档中有 7 个字段。我想将任何文档 CREATE 尝试限制为我期望的字段数量。我已将数字设置为 100,这显然不是我故意发送失败的数字。我预计 100 次会失败,但事实并非如此。
match /users/{uid} {
// Applies to writes to nonexistent documents
allow create: if request.resource.data.size() == 100;
}
我还尝试了以下方法,将 size 属性直接放置到资源对象上,但没有成功。
允许创建:if request.resource.size() == 100;
这是尝试 CREATE 操作的代码;
const DEFAULT_systemRole = 'User';
const DEFAULT_disabled = false;
// creates up a static sentinel value to be set on the Firestore server
var serverTimestamp = admin.firestore.FieldValue.serverTimestamp()
admin.firestore().collection('users').doc(req.body.uid).set({
// sent from client
usernameSlug: req.body.usernameSlug,
username: req.body.username,
email: req.body.email,
// set only here on server
systemRole: DEFAULT_systemRole,
disabled: DEFAULT_disabled,
dateModified: serverTimestamp,
dateCreated: serverTimestamp,
}).then(function() {
console.log("Successfully created FireStore user");
}).catch(function(error) {
console.log("Error creating user:", error);
throw(error)
})
最佳答案
关于node.js - Firestore 安全规则 : How to enforce the number of fields CREATED in a Firestore document thru security rules?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525444/