首页 > 解决方案 > firebase 规则应该如何在收集数据中找到并检查是否存在?

问题描述

创建新文档时(在我的示例中,这是配置文件文档),安全规则应检查包含此电子邮件的配置文件是否已存在。我创建了一个附加功能,它应该在“个人资料”集合中搜索所有带有特定电子邮件的个人资料,并返回是否已经收到电子邮件。

该文档提供了示例(链接):

service cloud.firestore {
  match /databases/{database}/documents/some_collection: {
    // Remember that, in Cloud Firestore, reads embedded in your rules are billed operations
    write: if request.auth != null && get(/databases/(database)/documents/users/$(request.auth.uid)).data.admin) == true;
    read: if request.auth != null;
  }
}

但我不能使用它,因为我没有特定的个人资料 ID,我需要通过电子邮件查找此集合中的所有个人资料。

我的代码:

match /profiles/{profileId} {
      allow create: if allowToCreateProfileWithEmail(request.resource.data.email);

    function allowToCreateProfileWithEmail (email) {

      let profiles = firebase.firestore().collection("profiles").where("email", "==", email).get();
      
      return profiles != null;
        
    }

但这是行不通的,因为firebase不存在。我怎样才能修复我的代码来解决我的问题?

标签: google-cloud-firestorefirebase-security

解决方案


推荐阅读