【问题标题】:Error Domain=FIRFirestoreErrorDomain Code=7 "Missing or insufficient permissions."错误域 = FIRFirestoreErrorDomain 代码 = 7“权限缺失或不足。”
【发布时间】:2020-10-02 23:19:54
【问题描述】:

所以我有以下代码从 firestore 检索文档,这个函数在我的 viewDidLoad() 中被调用:

func loadAnnotations() {
    
    db.collection("jumpSpotAnnotations").getDocuments { (querySnapshot, error) in
        if let error = error {
            print("There was an issue retrieving data from Firestore: \(error)")
            
            let alertController = UIAlertController(title: "Oops!", message: "We are having trouble loading the cliff jumping locations.  Be sure to check your internet connection.", preferredStyle: .alert)
            
            let okayAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
            
            alertController.addAction(okayAction)
            
            self.present(alertController, animated: true, completion: nil)
        } else {
            if let snapshotDocuments = querySnapshot?.documents {
                //print(snapshotDocuments)
                for doc in snapshotDocuments {

函数的其余部分并不重要。检索数据时出现错误:Error Domain=FIRFirestoreErrorDomain Code=7 "Missing or enough permissions."当我第一次得到这个时,我意识到这是由于我从创建应用程序的 Firebase 项目的第一天起就没有更新我的安全规则,并且已经过去了 30 天,所以谷歌无法访问了直到我更新了规则。这些是原始规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

为了这个应用程序的目的,现在我对用户创建和读取任何数据都很好。但是,我不希望他们能够修改或删除数据。所以我在 Stack 上问了一个关于如何做到这一点的问题,并在这里得到了回答:How to prevent Firestore documents being deleted / modified, but allow all reads and writes 所以我现在更新的安全规则是:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /collection1/{jumpSpotAnnotations} {
      allow read, create;
    }
  }
}

但是,当我尝试检索数据时,我仍然遇到完全相同的错误。新的安全规则已经发布。所以我的问题是,在按下发布后我需要做些什么来正式更新它,并让我的模拟器能够检索数据,或者是我的安全规则中的代码或 Xcode 中的函数不正确的问题为了我想做的事。请记住,Xcode 中的函数与原始规则完美配合(我在第一次创建 Firestore 项目时使规则完全开放,因此我可以轻松地在模拟器中进行测试)。非常感谢任何帮助!

【问题讨论】:

    标签: ios firebase google-cloud-firestore firebase-security


    【解决方案1】:

    在我对您上一个问题的回答中,我建议您说出各个集合的名称。你只是拿走了我给的东西并逐字复制。您需要在规则中使用实际集合的名称:

        match /jumpSpotAnnotations/{id} {
          allow read, create;
        }
    

    再次,我强烈建议您阅读我链接到的文档,以便了解安全规则的工作原理。

    【讨论】:

    • 我不是要逐字复制,抱歉。我真的认为'collection1'是我必须输入的东西,而{id}是集合的名称。我误解了你的解释。所以现在我知道 collection1 实际上是我的收藏名称,但我仍然不明白 {id} 是什么。或者“{id}”是我应该放在那里的字面意思吗?谢谢。是的,我将阅读文档,我只是在寻找快速修复,以便继续测试应用程序。编辑:现在我认为 {id} 指的是特定文档,在集合层次结构中更靠后?对吗?
    猜你喜欢
    • 2018-08-24
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2019-10-24
    • 2018-09-12
    • 2020-10-14
    • 2018-03-17
    相关资源
    最近更新 更多