在 Cloud Firestore 中如何创建一个只创建一次的安全规则
In Cloud Firestore how to make an, create only once security rule
我刚开始迁移到 Cloud Firestore,想知道这条安全规则。
在 Firebase 实时数据库中这条规则:
Evaluates to true if one operand in the rules expression is true.
In this example, we can write as long as old data or new data does not
exist. In other words, we can write if we're deleting or creating
data, but not updating data.
".write": "!data.exists() || !newData.exists()"
我正在尝试在 Cloud Firestore 中进行复制,例如
match /USER_ID/{Id} {
allow create: if resource.data.id != exist
allow read: if request.auth.uid != null;
}
我想要的是,如果 Document
存在于 USER_ID
Collection
中,那么发生这种情况的交易一定会失败。
但这是行不通的。我已阅读 doc 几遍,但无法正常工作
您需要使用 exist 函数,如果它存在,您需要将您正在测试的文档的路径传递给它。以下是您如何检查文档是否不存在的示例。
service cloud.firestore {
match /databases/{database}/documents/ {
match /USER_ID/{Id} {
allow create: if !exists(/databases/$(database)/documents/USER_ID/$(Id))
allow read: if request.auth.uid != null;
}
}
}
我刚开始迁移到 Cloud Firestore,想知道这条安全规则。 在 Firebase 实时数据库中这条规则:
Evaluates to true if one operand in the rules expression is true.
In this example, we can write as long as old data or new data does not exist. In other words, we can write if we're deleting or creating data, but not updating data.
".write": "!data.exists() || !newData.exists()"
我正在尝试在 Cloud Firestore 中进行复制,例如
match /USER_ID/{Id} { allow create: if resource.data.id != exist allow read: if request.auth.uid != null; }
我想要的是,如果 Document
存在于 USER_ID
Collection
中,那么发生这种情况的交易一定会失败。
但这是行不通的。我已阅读 doc 几遍,但无法正常工作
您需要使用 exist 函数,如果它存在,您需要将您正在测试的文档的路径传递给它。以下是您如何检查文档是否不存在的示例。
service cloud.firestore {
match /databases/{database}/documents/ {
match /USER_ID/{Id} {
allow create: if !exists(/databases/$(database)/documents/USER_ID/$(Id))
allow read: if request.auth.uid != null;
}
}
}