允许读取Firestore文档(如果不存在)

Dau*_*eDK 5 google-cloud-firestore firebase-security-rules

在Firestore中,如果目标文档不存在,我将不允许阅读。我尝试了以下方法:

service cloud.firestore {
   match /databases/{database}/documents {

     match /reports/{report} {
        allow read: if !exists(/databases/$(database)/documents/reports/{report});
     }

   }
}
Run Code Online (Sandbox Code Playgroud)

它不起作用,响应为:Missing or insufficient permissions.

Dav*_*der -1

如果您在服务器端执行此操作,您可以使用管理 SDK查询文档是否存在(firestore 规则仅适用于客户端 SDK):

const snapshot = await adminFirestore.doc('reports/non-existent-item').get();
if (!snapshot.exists) {
  // handle gracefully
}
Run Code Online (Sandbox Code Playgroud)